hdoj538Ponds【dfs+拓扑排序】

Ponds

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


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
 

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 number  p(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
  
  
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

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
struct Node{
	int to,next;
}A[200010];
bool vis[10010];
int head[10010];
int value[10010];
int cnt[10010];
int ans1,node;
long long ans2;
void add(int u,int v){
	A[node].to=v;
	A[node].next=head[u];
	head[u]=node++;
}
void topo(int k){
	while(1){
		bool flag=true;
		for(int i=head[k];i!=-1;i=A[i].next){
			cnt[A[i].to]--;
			if(cnt[A[i].to]==1){
				cnt[A[i].to]=-1;
				k=A[i].to;
				vis[A[i].to]=true;
				flag=false;
			}
			if(cnt[A[i].to]==0)vis[A[i].to]=true;
		}
		if(flag)break;
	}
}
void dfs(int k){
	for(int i=head[k];i!=-1;i=A[i].next){
		if(vis[A[i].to])continue;
		vis[A[i].to]=true;
		ans1++;ans2+=value[A[i].to];
		dfs(A[i].to);
	}
}
int main()
{
	int n,m,i,j,k,t;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;++i){
			scanf("%d",&value[i]);
		}
		int a,b;node=0;
		memset(cnt,0,sizeof(cnt));
		memset(head,-1,sizeof(head));
		for(i=0;i<m;++i){
			scanf("%d%d",&a,&b);
			add(a,b);add(b,a);
			cnt[a]++;cnt[b]++;
		}
		memset(vis,false,sizeof(vis));
		for(i=1;i<=n;++i){
			if(cnt[i]==0)vis[i]=true;
			if(!vis[i]&&cnt[i]==1){
				cnt[i]=-1;
				vis[i]=true;
				topo(i);
			}
		}
		long long ans=0;
		for(i=1;i<=n;++i){
			if(vis[i])continue;
			vis[i]=true;
			ans1=1;ans2=value[i];
			dfs(i);
			if(ans1&1){
				ans+=ans2;
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值