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
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
.
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
和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;
}