HDU 5438 Ponds(2015ACM长春网络赛+枚举删点+DFS求联通块)

来源:互联网 发布:帝国cms整站模板 编辑:程序博客网 时间:2024/05/16 16:20

Ponds

Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1026 Accepted Submission(s): 344


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

Input
The first line of input will contain a number T(1T30) 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(1p104) which represents the number of ponds she owns, and the other is the number m(1m105) which represents the number of pipes.

The next line contains p numbers v1,...,vp, where vi(1vi108) 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
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 

//分析:题目求尽可能的不断删掉边数小于2的点 最后求所有独立子块 如果点数为奇数则加起来 否则不加
//英语差真可怕  刚开始题意错了 直接跪了... 其实不难  首先不断求出边数小于2的点 删掉 并将连接的点减1  如果减后小于2 则进队 下次删掉 最后dfs求连通块
#include <iostream>#include <cstdio>#include <string.h>#include <string>#include <vector>#include <queue>using namespace std;const int N=10000+5;vector<int > ve[N];  //邻接表 bool book[N];   //标记删了没 bool flag[N];  //用于求连通dfs标记用  不用回朔 int edge[N];  //点的边数 long long num[N],sum;int n,m;queue<int> q;int child;  //用于记录每个联通块的点数 inline void del(){int i;for(i=1;i<=n;i++) if(edge[i]<2){ q.push(i); book[i]=1; }   while(!q.empty()){int now=q.front();q.pop();for(i=0;i<ve[now].size();i++){int x=ve[now][i];if(book[x]==1)  //删掉了的   continue;edge[x]--;if(edge[x]<2){book[x]=1;q.push(x);}}}}void dfs(int now){int i;for(i=0;i<ve[now].size();i++){int x=ve[now][i];if(book[x]==1||flag[x]==1)  continue;child++;sum+=num[x];flag[x]=1;dfs(x);}}int main(){int t,i,a,b;long long re;scanf("%d",&t);while(t--){re=0; scanf("%d%d",&n,&m);// while(!q.empty())  q.pop();  for(i=1;i<=n;i++) { scanf("%lld",&num[i]); book[i]=0; flag[i]=0;  edge[i]=0; ve[i].clear(); } for(i=0;i<m;i++){  scanf("%d%d",&a,&b); if(a==b)  continue; ve[a].push_back(b); ve[b].push_back(a); edge[a]++; edge[b]++; } del(); for(i=1;i<=n;i++){ if(book[i]==0&&flag[i]!=1){  //还没删  flag[i]=1; sum=num[i]; child=1; dfs(i); if(child%2)  re+=sum; } } printf("%lld\n",re);}return 0;}


0 0
原创粉丝点击