HDU 5438 Ponds
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 29 Accepted Submission(s): 13
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
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.
解题:拓扑排序
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 210000; 5 struct arc{ 6 int to,next; 7 arc(int x = 0,int y = -1){ 8 to = x; 9 next = y; 10 } 11 }e[600000]; 12 LL a[maxn]; 13 int tot,n,m,head[maxn],du[maxn]; 14 void add(int u,int v){ 15 e[tot] = arc(v,head[u]); 16 head[u] = tot++; 17 } 18 queue<int>q; 19 bool del[maxn]; 20 void topsort(){ 21 memset(del,false,sizeof del); 22 while(!q.empty()) q.pop(); 23 for(int i = 1; i <= n; ++i) 24 if(du[i] <= 1) {q.push(i);del[i] = true;} 25 while(!q.empty()){ 26 int u = q.front(); 27 q.pop(); 28 for(int i = head[u]; ~i; i = e[i].next){ 29 if(!del[e[i].to] && --du[e[i].to] <= 1){ 30 del[e[i].to] = true; 31 q.push(e[i].to); 32 } 33 } 34 } 35 } 36 LL bfs(int u){ 37 while(!q.empty()) q.pop(); 38 q.push(u); 39 bool odd = false; 40 LL ret = 0; 41 del[u] = true; 42 int cnt = 0; 43 while(!q.empty()){ 44 u = q.front(); 45 q.pop(); 46 cnt++; 47 ret += a[u]; 48 for(int i = head[u]; ~i; i = e[i].next){ 49 if(del[e[i].to]) continue; 50 del[e[i].to] = true; 51 q.push(e[i].to); 52 } 53 } 54 return (cnt&1)?ret:0; 55 } 56 int main(){ 57 int kase,u,v; 58 scanf("%d",&kase); 59 while(kase--){ 60 scanf("%d%d",&n,&m); 61 memset(head,-1,sizeof head); 62 memset(du,0,sizeof du); 63 tot = 0; 64 for(int i = 1; i <= n; ++i) 65 scanf("%I64d",a + i); 66 for(int i = 0; i < m; ++i){ 67 scanf("%d%d",&u,&v); 68 add(u,v); 69 add(v,u); 70 du[u] += 1; 71 du[v] += 1; 72 } 73 topsort(); 74 LL ret = 0; 75 for(int i = 1; i <= n; ++i) 76 if(!del[i]) ret += bfs(i); 77 printf("%I64d\n",ret); 78 } 79 return 0; 80 }
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 开发者新选择:用DeepSeek实现Cursor级智能编程的免费方案
· Tinyfox 发生重大改版
· 独立开发经验谈:如何通过 Docker 让潜在客户快速体验你的系统
· 小米CR6606,CR6608,CR6609 启用SSH和刷入OpenWRT 23.05.5
· 近期最值得关注的AI技术报告与Agent综述!
2014-09-13 HDU 2639 Bone Collector II
2014-09-13 POJ 2516 Minimum Cost
2014-09-13 POJ 1459 Power Network