HDU - 5438 (拓扑排序+dfs)

网友投稿 274 2022-08-30

HDU - 5438 (拓扑排序+dfs)

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 vv.  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)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)p(1≤p≤104) which represents the number of ponds she owns, and the other is the number m(1≤m≤105)m(1≤m≤105) which represents the number of pipes.  The next line contains pp numbers v1,...,vpv1,...,vp, where vi(1≤vi≤108)vi(1≤vi≤108) indicating the value of pond ii.  Each of the last mm lines contain two numbers aa and bb, which indicates that pond aaand pond bb 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

题目大意:

给出n个点,每个点都有一个价值w,给出m条边,要求先把度数小于2的点删除,然后计算联通块的点为奇数的价值和。

思路:

首先删除点可以用拓扑排序,标记一下。

然后找联同块可以用dfs跑一边。求和。

代码:

#include #include #include #include #include #include #include #include #include #include using namespace std;#define ll long longconst int maxn = 2e5+100;const int maxm = 1e5 + 100;int n, m;int in[maxn],vis[maxn];struct Edge{ int to, next; Edge() {} Edge(int t,int n) { to = t; next = n; }}edges[maxn];int head[maxn], tot;void add_edge(int from, int to){ edges[tot] = Edge ( to, head[from] ); head[from] = tot++; in[to]++;}bool topo(){ queue Q; int sum = 0; for (int i = 1;i <= n;i++)if (in[i] < 2) Q.push(i); while (!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 1; sum++; for (int e = head[u];e != -1;e = edges[e].next) { int v = edges[e].to; if (--in[v] < 2&&!vis[v]) { Q.push(v); } } } return sum;}int w[maxn];void init(){ memset(in, 0, sizeof(in)); memset(w, 0, sizeof(w)); memset(vis, 0, sizeof(vis)); memset(head, -1, sizeof(head)); tot = 0;}ll ans;int dfs(int u, int fa){ ans = ans + w[u]; int size = 1; for (int e = head[u];e != -1;e = edges[e].next) { int v = edges[e].to; if (v == fa)continue; if (vis[v])continue; vis[v] = 1; size+=dfs(v, u); } return size;}int main(){ int t; scanf("%d", &t); while (t--) { init(); scanf("%d%d", &n, &m); for (int i = 1;i <= n;i++) { scanf("%d", &w[i]); } for (int i = 1;i <= m;i++) { int u, v; scanf("%d%d", &u, &v); add_edge(u, v); add_edge(v, u); } topo(); ll sum = 0; for (int i = 1;i <= n;i++) { if (vis[i])continue; vis[i] = 1; ans = 0; int flag=dfs(i, -1); if(flag%2==1) { sum += ans; } } printf("%lld\n", sum); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:拉面说,患了“营销依赖症”?(拉面说营销分析)
下一篇:处女座和小姐姐(三)(数位dp)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~