HDU 5438 Ponds (拓撲排序應用+DFS)
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1292 Accepted Submission(s): 429
Problem Description
Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and there will not be more than one pipe between two ponds. Each pond has a value
v.
Now Betty wants to remove some ponds because she does not have enough money. But each time when she removes a pond, she can only remove the ponds which are connected with less than two ponds, or the pond will explode.
Note that Betty should keep removing ponds until no more ponds can be removed. After that, please help her calculate the sum of the value for each connected component consisting of a odd number of ponds
Now Betty wants to remove some ponds because she does not have enough money. But each time when she removes a pond, she can only remove the ponds which are connected with less than two ponds, or the pond will explode.
Note that Betty should keep removing ponds until no more ponds can be removed. After that, please help her calculate the sum of the value for each connected component consisting of a odd number of ponds
Input
The first line of input will contain a number
T(1≤T≤30)
which is the number of test cases.
For each test case, the first line contains two number separated by a blank. One is the number p(1≤p≤104) which represents the number of ponds she owns, and the other is the number m(1≤m≤105) which represents the number of pipes.
The next line contains p numbers v1,...,vp, where vi(1≤vi≤108) indicating the value of pond i.
Each of the last m lines contain two numbers a and b, which indicates that pond a and pond b are connected by a pipe.
For each test case, the first line contains two number separated by a blank. One is the number p(1≤p≤104) which represents the number of ponds she owns, and the other is the number m(1≤m≤105) which represents the number of pipes.
The next line contains p numbers v1,...,vp, where vi(1≤vi≤108) indicating the value of pond i.
Each of the last m lines contain two numbers a and b, which indicates that pond a and pond b are connected by a pipe.
Output
For each test case, output the sum of the value of all connected components consisting of odd number of ponds after removing all the ponds connected with less than two pipes.
Sample Input
1
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7
Sample Output
21
Source
2015 ACM/ICPC Asia Regional Changchun Online
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=5438
題目大意:一個圖,不斷刪去度數小於2的點,求剩下的點數為奇數的連通子圖的點數和
題目分析:拓撲排序刪點,DFS算和
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=5438
題目大意:一個圖,不斷刪去度數小於2的點,求剩下的點數為奇數的連通子圖的點數和
題目分析:拓撲排序刪點,DFS算和
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define ll long long
using namespace std;
int const MAXM = 1e5 + 5;
int const MAXN = 1e4 + 5;
int n, m, cnt, num;
int head[MAXN], deg[MAXN];
bool vis[MAXN];
ll tmp, ans, val[MAXN];
struct EDGE
{
int to, next;
}e[MAXM];
queue <int> q;
void Init()
{
while(!q.empty())
q.pop();
cnt = 0;
ans = 0;
memset(head, -1, sizeof((head)));
memset(deg, 0, sizeof(deg));
memset(vis, false, sizeof(vis));
}
void Add(int u, int v)
{
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt ++;
}
void TopoSort()
{
for(int i = 1; i <= n; i++)
{
if(deg[i] == 0)
vis[i] = true;
if(deg[i] == 1)
{
vis[i] = true;
q.push(i);
}
}
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = head[u]; i != -1; i = e[i].next)
{
int v = e[i].to;
deg[v] --;
if(deg[v] == 0)
vis[v] = true;
if(deg[v] == 1)
{
vis[v] = true;
q.push(v);
}
}
}
}
void DFS(int u, int fa)
{
vis[u] = true;
num ++;
tmp += val[u];
for(int i = head[u]; i != -1; i = e[i].next)
{
int v = e[i].to;
if(!vis[v] && v != fa)
DFS(v, u);
}
return;
}
int main()
{
int T;
scanf("%d", &T);
while(T --)
{
Init();
int u, v;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++)
scanf("%I64d", &val[i]);
for(int i = 1; i <= m; i++)
{
scanf("%d %d", &u, &v);
Add(u, v);
Add(v, u);
deg[u] ++;
deg[v] ++;
}
TopoSort();
for(int i = 1; i <= n; i++)
{
if(!vis[i])
{
num = 0;
tmp = 0;
DFS(i, 0);
if(num & 1)
ans += tmp;
}
}
printf("%I64d\n", ans);
}
}
相關文章
- DFS實現拓撲排序排序
- 有向圖的拓撲排序——DFS排序
- 拓撲排序詳解(梅開二度之dfs版按字典序輸出拓撲路徑+dfs版輸出全部拓撲路徑排序
- 拓撲排序排序
- 拓撲排序,YYDS排序
- 筆記:拓撲排序筆記排序
- 拓撲排序小結排序
- 圖論——拓撲排序圖論排序
- 【筆記/模板】拓撲排序筆記排序
- AOV網與拓撲排序排序
- Reward (圖論+拓撲排序)圖論排序
- 拓撲排序就這麼回事排序
- 演算法-圖論-拓撲排序演算法圖論排序
- (set+拓撲排序) CF1572A Book排序
- 圖解拓撲排序+程式碼實現圖解排序
- Mr. Kitayuta‘s Technology CodeForces - 505D(並查集+拓撲排序或dfs找環) 題解並查集排序
- 1385E. Directing Edges(拓撲序的應用)
- 拓撲排序 (BFS )DAG (有向無環圖)排序
- 圖的拓撲排序詳解與實現排序
- 【Tarjan 拓撲排序 dp】P3387 【模板】縮點排序
- VOL.2 拓撲排序與關鍵路徑排序
- Noc拓撲
- 使用 eBPF 零程式碼修改繪製全景應用拓撲eBPF
- 基於 HTML5 Canvas 的拓撲元件 ToolTip 應用HTMLCanvas元件
- 牛客 51011 可達性統計(拓撲排序,bitset)排序
- 洛谷P3953 逛公園(dp 拓撲排序)排序
- 【BZOJ-1565】植物大戰殭屍 拓撲排序 + 最小割排序
- Day2 尤拉路,拓撲排序和差分約束排序
- HDU1427速算24點(dfs)
- HDU - 2553 N皇后問題(DFS)
- HDU 1427-速算24點(DFS)
- Leetcode 1691. 堆疊長方體的最大高度(拓撲排序 + DP)LeetCode排序
- hdu 6446 Tree and Permutation(dfs+思維)
- 網路拓撲結構
- StratoVirt 的 vCPU 拓撲(SMP)
- BZOJ2535: [Noi2010]Plane 航空管制2(拓撲排序 貪心)排序
- Android程式設計師會遇到的演算法(part 7 拓撲排序)Android程式設計師演算法排序
- 網路拓撲圖:網路拓撲圖介紹及線上製作
- 演算法資料結構 | 圖論基礎演算法——拓撲排序演算法資料結構圖論排序