HDU 5438 Ponds
来源:互联网 发布:37传奇霸业炼体数据 编辑:程序博客网 时间:2024/05/16 16:11
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 valuev .
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
Sample Output
21
Source
2015 ACM/ICPC Asia Regional Changchun Online
/*
题目大意: 给定P个池塘的价值,M个再各个池塘之间相连的管道。问去掉一些边之后,剩下所有由奇数个点组成的环(奇数环)的总价值是多少。
网络赛中比较简单的题目,却不知道该怎么去实现,顺便学习了之前一直没有接触的前向星。思路,拓扑排序找环,用dfs找奇数环。
*/
/*
题目大意: 给定P个池塘的价值,M个再各个池塘之间相连的管道。问去掉一些边之后,剩下所有由奇数个点组成的环(奇数环)的总价值是多少。
网络赛中比较简单的题目,却不知道该怎么去实现,顺便学习了之前一直没有接触的前向星。思路,拓扑排序找环,用dfs找奇数环。
*/
#include <algorithm>#include <iostream>#include <numeric>#include <cstring>#include <iomanip>#include <string>#include <vector>#include <cstdio>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>#define LL long longconst int M = 10010;const double esp = 1e-6;const int INF = 0x3f3f3f3f;const double PI = 3.141592653589793;using namespace std;struct node{ int u,v; int next;}ls[M*20];int hd[M],val[M],kp[M];int p,m,top;LL flag,sum;bool vis[M];queue<int>Q;void build(int x,int y){ ls[top].v = y; ls[top].next = hd[x]; hd[x] = top++;}void grest(){ for(int i=1;i<=p;i++){ if(kp[i] < 2){ vis[i] = true; Q.push(i); } } int u,v; while(!Q.empty()){ u = Q.front();Q.pop(); for(int i=hd[u];i!=-1;i=ls[i].next){ v = ls[i].v; if(!vis[v]){ kp[v]--; if(kp[v] < 2){ vis[v] = true; Q.push(v); } } } }}void dfs(int x){ flag++; vis[x] = true; sum+=(LL)val[x]; for(int i=hd[x];i!=-1;i=ls[i].next){ if(!vis[ls[i].v]){ dfs(ls[i].v); } }}int main(){ int T,u,v; while(~scanf("%d",&T)){ while(T--){ memset(hd,-1,sizeof(hd)); memset(kp,0,sizeof(kp)); memset(vis,false,sizeof(vis)); scanf("%d %d",&p,&m); while(!Q.empty()) Q.pop(); top = 0; for(int i=1;i<=p;i++){ scanf("%d",&val[i]); } for(int i=1;i<=m;i++){ scanf("%d %d",&u,&v); build(u,v);build(v,u); kp[u]++;kp[v]++; } grest(); LL ans = 0; for(int i=1;i<=p;i++){ if(!vis[i]){ flag = sum = 0; dfs(i); if(flag%2) ans+=sum; } } cout<<ans<<endl; } } return 0;}
0 0
- hdu 5438 Ponds DFS
- HDU 5438 Ponds
- hdu 5438Ponds
- HDU 5438 Ponds
- HDU 5438 Ponds
- HDU 5438 Ponds
- HDU 5438 Ponds
- HDU 5438Ponds
- HDU 5438Ponds
- HDU 5438 Ponds
- hdu 5438 Ponds(toposort+DFS)
- HDU 5438 Ponds (搜索)
- HDU 5438 Ponds dfs模拟
- Ponds HDU
- HDU 5438 Ponds 拓扑+并查集
- hdu 5438 Ponds(强连通)
- hdu 5438 Ponds(线段树+dfs)
- hdu 5438 Ponds(拓扑应用、DFS)
- 关于servlet的生命周期
- 使用commons-beanutils把javabean转换成Map日期date不能自定义格式
- 【线性表项目2 - “顺序表”算法库终极完整版】
- javaweb2
- Winform 调用WCF客户端,所有服务端方法在运行的时候均找不到(编译没有问题)
- HDU 5438 Ponds
- javaweb3
- NetBeans IDE中实现页面跳转的 LoginFrame类
- C语言之空宏
- HTML5 重力感应 事件讲解
- javaweb6
- Spring xml注入实例
- javaweb7
- 把别的项目导入myeclipse中出现的错误