您的位置:首页 > 其它

HDU 5438(拓扑排序+并查集)

2015-09-28 21:12 309 查看


Ponds
[b]Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 2334    Accepted Submission(s): 738
[/b]

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(1≤T≤30)
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(1≤p≤104)
which represents the number of ponds she owns, and the other is the number
m(1≤m≤105)
which represents the number of pipes.

The next line contains p
numbers v1,...,vp,
where vi(1≤vi≤108)
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
2015 ACM/ICPC Asia Regional Changchun Online

 
题目大意:n个顶点m条边的无向图,每个顶点有一个权值,现在要删除所有的叶子节点,重复此操作直至形成的“森林”中不存在叶子节点,然后找出森林中顶点个数为奇数的树的权值的和。
#include<iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=100010;
int u[maxn],v[maxn];
int w[maxn],ind[maxn];
bool vis[maxn];
int p[maxn],ran[maxn];  //p数组为父亲,ran记录有联通块的里面的东西个数
ll sum[maxn];  //记录联通块的权值。
struct node
{
int to,next;
} edge[maxn*20];
int head[maxn],num;
void add(int u,int v)
{
edge[num].to=v;
edge[num].next=head[u];
head[u]=num++;
}
int find(int x)
{
if(p[x]!=x)
return p[x]=find(p[x]);
return x;
}
void hebing(int a,int b)
{
int x=find(a);
int y=find(b);
if(x==y)
return;
p[y]=x;
ran[x]+=ran[y];
sum[x]+=sum[y];
}
void init(int n)
{
for(int i=1; i<=n; i++)
{
p[i]=i;
ran[i]=1;
sum[i]=w[i];
}
}
void to_sort(int n)
{
int i;
queue<int>q;
for(i=1; i<=n; i++)
{
if(ind[i]<=1)
{
q.push(i);
vis[i]=0;
}
}
while(!q.empty())
{
int cur=q.front();
q.pop();
for(i=head[cur]; i!=-1; i=edge[i].next)
{
int xx=edge[i].to;
if(vis[xx])
{
ind[xx]--;
if(ind[xx]==1)
{
q.push(xx);
vis[xx]=0;
}
}
}
}
}
int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
int n,m;
num=0;
memset(vis,1,sizeof(vis));
memset(head,-1,sizeof(head));
memset(ind,0,sizeof(ind));
scanf("%d%d",&n,&m);
for(i=1; i<=n; i++)
scanf("%d",&w[i]);
for(i=1; i<=m; i++)
{
scanf("%d%d",&u[i],&v[i]);
add(u[i],v[i]);
add(v[i],u[i]);
ind[u[i]]++;
ind[v[i]]++;
}
to_sort(n);
init(n);
for(i=1; i<=m; i++)
{
if(vis[u[i]] &&vis[v[i]])
hebing(u[i],v[i]);
}
ll ans=0;
for(i=1; i<=n; i++)
{
if(p[i]==i &&vis[i] &&ran[i]%2==1)
ans+=sum[i];
}
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM算法