HDU - 5438 (并查集)

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

和zxy一起改掉bug改出来的真的很开始 

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1e4+10;
int pond[maxn];
int pre[maxn];
int num[maxn];
int cnt[maxn];
vector<int>vec[maxn];
int findd(int x){
	if(x==pre[x])return x;
	else return pre[x]=findd(pre[x]);
}
void init(int n)
{
	for(int i=1;i<=n;i++)
	{
		cnt[i]=0;
		num[i]=0;
		pre[i]=i;
		vec[i].clear();
	}
}
void merge(int x,int y){
	int fx=findd(x);
	int fy=findd(y);
	if(fx!=fy)
	pre[fx]=fy;
}
int main()
{
	int t;cin>>t;
	int p,m,x,y;
	while(t--)
	{
		scanf("%d%d",&p,&m);
		init(p);
		for(int i=1;i<=p;i++)
			scanf("%d",&pond[i]); //表示价值
		for(int i=1;i<=m;i++){
			scanf("%d%d",&x,&y);
			merge(x,y);//并查集连接
			num[x]++;num[y]++;//表示每一个池塘有多少和他连接的
			vec[x].push_back(y);//表示连接的那个是什么
			vec[y].push_back(x);
		}
		for(int i=1;i<=p;i++)
			cnt[findd(i)]++;//记录下这个来表示每一个集合都有多少个池塘
		//输入的所有事情都已经处理好了 现在要做的就是去掉小于2的东西
            for(int i=1;i<=p;i++)
            {
				if(num[i]==0)
				{
					cnt[pre[i]]=0;
					pre[i]=-1;
				}else if(num[i]==1){
				    int tt=i;
				    while(num[tt]==1)
                    {
                        cnt[pre[tt]]--;
                        num[tt]=0;
                        pre[tt]=-1;
                        int gg=vec[tt][0];
                        vec[tt].clear();
                        num[gg]--;
                        vector<int>::iterator it=vec[gg].begin();
                        while(it!=vec[gg].end()){
                           if(*it==tt)
                           {
                                vec[gg].erase(it);
                                break;
                           }
                            it++;
                        }
                        tt=gg;
                    }
				}
            }

		long long sum=0;
		for(int i=1;i<=p;i++){
		    int p=pre[i];
		    if(p==-1)continue;
            if(cnt[p]%2==1){
            sum+=pond[i];
            }
		}
		printf("%lld\n",sum);
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值