Ponds
HDU - 5438Betty 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.
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
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 7Sample Output
21
题目大意:n个顶点m条边的无向图,每个顶点有一个权值,现在要删除所有的叶子节点,重复此操作直至形成的“森林”中不存在叶子节点,然后找出森林中顶点个数为奇数的树的权值的和。
分析:删除操作可以模拟拓扑过程,首先我们让度数为1的顶点进队,然后每出队一个顶点,就让该顶点相邻顶点的度数减1,碰到度数为1的顶点就入队,重复操作直至队列为空,这样我们就删除了所有的叶子节点,此时图可能变为了森林,然后只需在剩下的森林中找出符合条件的树即可,可以用并查集纪录每个树中顶点的个数和树的权值。要注意的是图不能用邻接矩阵来存储会内存超限,这里用的是前向星。
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 10005;
int u[maxn],v[maxn];//记录每条边
int w[maxn],degree[maxn];//w记录每个点权值,degree记录每个点度数
int vis[maxn];
struct node{
int to;
int nex;
}edge[maxn*20];
int head[maxn],num;
void add(int u,int v){
edge[num].to = v;
edge[num].nex = head[u];
head[u] = num++;
}
//并查集
int pre[maxn],cnt[maxn];//pre记录父亲,cnt记录个数
ll sum[maxn];//sum记录总权值
void init(int n){
for(int i = 1; i <= n; i++){
pre[i] = i;
cnt[i] = 1;
sum[i] = w[i];
}
}
int Find(int x){
if(x == pre[x])
return x;
else return pre[x] = Find(pre[x]);
}
void Union(int a,int b){
int x = Find(a),y = Find(b);
if(x != y){
pre[y] = x;
cnt[x] += cnt[y];
sum[x] += sum[y];
}
}
void Top(int n){
queue<int> que;
for(int i = 1; i <= n; i++){
if(degree[i] <= 1){
vis[i] = 1;
que.push(i);
}
}
while(!que.empty()){
int tmp = que.front();
que.pop();
for(int i = head[tmp]; i != -1; i = edge[i].nex){
int x = edge[i].to;
if(!vis[x]){
degree[x]--;
if(degree[x] == 1){
vis[x] = 1;
que.push(x);
}
}
}
}
}
int main(){
int t,m,n;
scanf("%d",&t);
while(t--){
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head));
memset(degree,0,sizeof(degree));
num = 0;
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++){
scanf("%d",&w[i]);
}
for(int i = 0; i < m; i++){
scanf("%d%d",&u[i],&v[i]);
add(u[i],v[i]);
add(v[i],u[i]);
degree[u[i]]++;
degree[v[i]]++;
}
Top(n);
init(n);
for(int i = 0; i < m; i++){
if(!vis[u[i]] && !vis[v[i]])
Union(u[i],v[i]);
}
ll ans = 0;
for(int i = 1; i <= n; i++){
if(i == pre[i] && !vis[i] && (cnt[i] & 1))
ans += sum[i];
}
printf("%lld\n",ans);
}
return 0;
}