hdu5438 Ponds dfs 2015changchun网络赛
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 533 Accepted Submission(s): 175
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <queue> using namespace std; int p, m; const int M = 100005; const int N = 10005; typedef long long ll; struct edge { int v, to; edge() { }; edge( int v, int to): v(v), to(to) {}; } e[M]; int head[N], flag[N], val[N], in[N], vis[N], tot; queue< int > q; void init() { memset (head, -1, sizeof head); memset (flag, 0, sizeof flag); memset (vis, 0, sizeof vis); memset (in, 0, sizeof in); tot = 0; while (!q.empty()) q.pop(); } void addedge( int u, int v) { e[tot] = edge(v, head[u]); head[u] = tot++; } void dfs( int u) { for ( int i = head[u]; i != -1; i = e[i].to) { int v = e[i].v; if (in[v] == 0) continue ; if (flag[v]) continue ; in[v]--; in[u]--; if (in[v] == 1) { q.push(v); flag[v] = 1; } } } void pre() { for ( int i = 1; i <= p; ++i) { if (in[i] == 1) { flag[i] = 1; q.push(i); } } while (!q.empty()) { int f = q.front(); q.pop(); dfs(f); } } int cnt; ll sum; void calc( int u) { cnt++; vis[u] = 1; sum += val[u]; for ( int i = head[u]; i != -1; i = e[i].to) { int v = e[i].v; if (vis[v]) continue ; if (flag[v]) continue ; calc(v); } } int main() { int _; scanf ( "%d" , &_); while (_ --) { scanf ( "%d%d" , &p, &m); int u, v; for ( int i = 1; i <= p; ++i) scanf ( "%d" , &val[i]); init(); for ( int i = 0; i < m; ++i) { scanf ( "%d%d" , &u, &v); in[u]++; in[v]++; addedge(u, v); addedge(v, u); } pre(); ll ans = 0; for ( int i = 1; i <= p; ++i) if (!flag[i] && !vis[i]) { cnt = 0; sum = 0; calc(i); if ((cnt & 1) && cnt !=1) ans += sum; } printf ( "%lld\n" , ans); } return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧