HDU 5438 Ponds(并查集)

思路:不断删除度数为1的点可以类似拓扑排序那样删除,然后并查集合并的时候统计一下顶点数和权值和就可以了


#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 10000+7;
vector<int>e[maxn];
int w[maxn],degree[maxn],fa[maxn],vis[maxn],cnt[maxn];
LL sum[maxn];
int n,m;
int Find(int x){return x==fa[x]?x:fa[x]=Find(fa[x]);}
void merge(int u,int v)
{
	int uu = Find(u);
	int vv = Find(v);
	if(uu!=vv)
	{
		fa[uu]=vv;
		sum[vv]+=sum[uu];
		cnt[vv]+=cnt[uu];
	}
}
void init()
{
	for(int i = 0;i<=n;i++)
		e[i].clear();
	memset(degree,0,sizeof(degree));
	memset(vis,0,sizeof(vis));
	for(int i = 1;i<=n;i++)
	{
        fa[i]=i;
		sum[i]=w[i];
		cnt[i]=1;
	}
}
void topo()
{
	queue<int>q;
    for(int i = 1;i<=n;i++)
		if(degree[i]<=1)
		{
			vis[i]=1;
			q.push(i);
		}
	while(!q.empty())
	{
		int u = q.front();
		q.pop();
		for(int i = 0;i<e[u].size();i++)
		{
			int v = e[u][i];
			if(--degree[v]<=1 && !vis[v])
			{
				q.push(v);
				vis[v]=1;
			}
		}
	}
}
int main()
{
    int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		for(int i = 1;i<=n;i++)
			scanf("%d",&w[i]);
		init();
		for(int i = 1;i<=m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			e[u].push_back(v);
			e[v].push_back(u);
			degree[u]++,degree[v]++;
		}
		topo();
        for(int u = 1;u<=n;u++)
		{
			if(!vis[u])
			{
                for(int i = 0;i<e[u].size();i++)
				{
					int v = e[u][i];
					if(!vis[v])
						merge(u,v);
				}
			}
		}
		LL ans = 0;
		for(int u = 1;u<=n;u++)
		{
             if(Find(u)==u && (cnt[u]&1) && !vis[u])
				 ans+=sum[u];
		}
		printf("%lld\n",ans);
	}
}


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 

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   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   which represents the number of ponds she owns, and the other is the number   which represents the number of pipes. 

The next line contains   numbers  , where   indicating the value of pond 

Each of the last   lines contain two numbers   and  , which indicates that pond   and pond   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



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值