hdu 5438 Ponds 拓扑排序 + 并查集

Ponds

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


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
 

题意:  给你n 个池塘  然后m 个管道。 但是主人 支付不起  所有池塘的 费用  ,所以他想删除一些池塘 ,但是
删除一个池塘的条件就是他的入度要小于等于1  删除后  可能会形成 x  个联通分支   问你 联通分支中点为奇数
的价值和为多少 。    样例中  删除7  那么形成两个点为3  的联通分支  那么价值就是 1,2,3,4,5,6 的和。
思路: 当然我们先要找出删除完所有入度小于等于1 的点后的情况。  注意如果删除这个点后  与他相联的点的入度也会发生改变  那么 如果 入度为1  那么就肯定也可以删除。  然后  我们用并查集维护 联通分支的个数。然后就完了。


#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<algorithm>
#define N 10005

using namespace std;
typedef long long ll;

int val[N];
int vis[N];
int inde[N];
vector<int> ve[N];
vector< int > fin[N];
int f[N];
int n,m;

int getf(int x)
{
	return f[x]==x?x:(f[x]=getf(f[x]));
}

void merge(int x,int y)
{
	int t1=getf(x);
	int t2=getf(y);
	if(t1!=t2)
	{
		f[t2]=t1;
	}
	return ;
}

void topo()
{
	queue< int >que;
	
	/*
	for(int i=1;i<=n;i++) cout<<inde[i]<<" ";
	//cout<<endl;
	*/
	
	for(int i=1;i<=n;i++)
	{
		if(inde[i]==0) vis[i]=1;
		else if(inde[i]==1){
			que.push(i);
		}
	}
	
	int u,v;
	
	while(!que.empty())
	{
		u=que.front();  que.pop();
		vis[u]=1;
		int sz=ve[u].size();
		for(int i=0;i<sz;i++)
		{
			v=ve[u][i];
			inde[v]--;
			if(inde[v]==1)
			{
				que.push(v);
			}
		}
	}
	
	for(int i=1;i<=n;i++)
	{
		if(vis[i]||inde[i]==1|inde[i]==0) continue;
		for(int j=0;j<ve[i].size();j++)
		{
			int v=ve[i][j];
			if(vis[v]||inde[v]==0||inde[v]==1) continue;
			merge(i,v);
		}
	}
	
	for(int i=1;i<=n;i++)
	{
		if(vis[i]||inde[i]==1||inde[i]==0) continue;
		int fa=getf(i);
		fin[fa].push_back(i);
	}
	
	ll ans=0;
	
	for(int i=1;i<=n;i++)
	{
		int sz=fin[i].size();
		if(sz>=2&&(1&sz))
		{
			for(int j=0;j<sz;j++)
			{
				ans+=val[fin[i][j]];
			}
		}
	}
	
	
	cout<<ans<<endl;
	return ;
}

void init()
{
	memset(vis,0,sizeof(vis));
	memset(inde,0,sizeof(inde));
	for(int i=1;i<=n;i++) f[i]=i;
	for(int i=1;i<=n;i++) ve[i].clear(),fin[i].clear();
	return ;
}

int main()
{
	int cas;
	int x,y;
	cin>>cas;
	while(cas--)
	{
		cin>>n>>m;
		
		init();
		for(int i=1;i<=n;i++) cin>>val[i];
		for(int i=1;i<=m;i++)
		{
			cin>>x>>y;
			inde[x]++;  inde[y]++;
			ve[x].push_back(y);
			ve[y].push_back(x);
		}
		
		topo();
		
	}
	return 0;
} 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值