HDU 5438.Ponds【2015 ACM/ICPC Asia Regional Changchun Online】【DFS】9月13
来源:互联网 发布:apache 源码下载linux 编辑:程序博客网 时间:2024/05/22 12:47
Ponds
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 numberp(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 containsp numbers v1,...,vp , where vi(1≤vi≤108) indicating the value of pond i .
Each of the lastm 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
The next line contains
Each of the last
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
17 71 2 3 4 5 6 71 41 54 52 32 63 62 7
#include<cstdio>#include<cstring>#include<vector>#include<iostream>using namespace std;vector <int> n[10010];int g[10010],f[10010],ns[10010],xx;//xx已经遍历的节点个数long long sum,sum2;void DFS(int z){//深搜 xx++; f[z]=1; sum2+=g[z]; for(int i=0;i<n[z].size();i++) if(!f[n[z][i]]) DFS(n[z][i]);}int main(){ int T,p,m,x,y; scanf("%d",&T); while(T--){ sum = xx = 0; memset(f,0,sizeof(f));//初始化 memset(g,0,sizeof(g));//初始化 memset(ns,0,sizeof(ns));//初始化 for(int i=0;i<10010;i++) n[i].clear();//初始化 scanf("%d%d",&p,&m); for(int i=1;i<=p;i++) scanf("%d",&g[i]); for(int i=0;i<m;i++){ scanf("%d%d",&x,&y); n[x].push_back(y); n[y].push_back(x); ns[x]++; ns[y]++; } bool key=true; while(key){ key=false; for(int i=1;i<=p;i++) if(ns[i]==1||ns[i]==0){ key=true; f[i]=1;//当作已经遍历过 for(int j=0;j<n[i].size();j++) ns[n[i][j]]--; ns[i]=-1; } } for(int i=1;i<=p;i++){ if(!f[i]){ sum2=xx=0; DFS(i); if(xx%2==1)//连通奇数个 sum+=sum2; } } printf("%lld\n",sum); } return 0;}
0 0
- HDU 5438.Ponds【2015 ACM/ICPC Asia Regional Changchun Online】【DFS】9月13
- hdu 5438 Ponds 2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5438 Ponds(dfs)——2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5438 Ponds (拓扑排序+DFS)2015 ACM/ICPC Asia Regional Changchun Online
- hdu 5438 Ponds 拓扑排序+并查集 2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5441.Travel【2015 ACM/ICPC Asia Regional Changchun Online】【并查集】9月18
- 2015 ACM/ICPC Asia Regional Changchun Online hdu 5438
- HDU 5441 Travel(2015 ACM/ICPC Asia Regional Changchun Online)
- 2015 ACM/ICPC Asia Regional Changchun Online
- 2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5437.Alisha’s Party【2015 ACM/ICPC Asia Regional Changchun Online】【优先队列】9月14
- HDU 5444.Elven Postman【2015 ACM/ICPC Asia Regional Changchun Online】【二叉树建立与遍历】9月14
- 2012 ACM/ICPC Asia Regional Changchun Online(hdu 4267 - 4277)线段树+dp+dfs+树形dp
- 2013 ACM/ICPC Asia Regional Changchun Online hdu Theme Section
- 2012 ACM/ICPC Asia Regional Changchun Online
- 2013 ACM/ICPC Asia Regional Changchun Online
- 2013 ACM/ICPC Asia Regional Changchun Online
- 2013 ACM/ICPC Asia Regional Changchun Online
- Unity 3D 泰课网小球移动 相机跟随
- 牛人博客
- linux 实现cat命令
- QString与int、string的相互转换
- HDU 5446 Unknown Treasure(Lucas定理+中国剩余定理)
- HDU 5438.Ponds【2015 ACM/ICPC Asia Regional Changchun Online】【DFS】9月13
- 第二周上机实践项目2——C/C++语言中函数参数传递的三种方式
- Configure multiple view resolvers priority in Spring MVC
- screen pinning
- 数据结构—Java语言判断单链表是否有环与寻找结环节点
- Hacker之路技能树(2)
- [解决]dyld: Library not loaded: @rpath/libswiftCore.dylib / Image not found
- hibernate主键生成策略 @GeneratedValue 与 @GenericGenerator
- 经典的冒泡排序