Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1905 Accepted Submission(s): 602
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
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.
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
Recommend
hujie | We have carefully selected several similar problems for you: 5449 5448 5447 5446 5445
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
#define SIZE 10005
int val[SIZE];
int pond_npipe[SIZE];
//int set[SIZE];
int poi[SIZE];
vector<int> node[SIZE];
queue<int> one_pipe;
//DFS
long long sum_val,sum_num;
void dfs(int i)
{
for(int j=0; j<node[i].size(); ++j)
{
if(!poi[node[i][j]])
{
sum_num++;
sum_val+=val[ node[i][j] ];
// cout<<"val[ node[i][j] ]:"<<val[ node[i][j] ]<<endl;;
// cout<<"this "<<sum_val<<endl;;
poi[ node[i][j] ]=1;
dfs(node[i][j]);
}
}
}
/*
int set_find(int x)
{
if(set[x]==-1)return x;
return set[x]=set_find(set[x]);
}
void set_join(int x, int y)
{
x=set_find(x);
y=set_find(y);
if(x!=y)
{
set[x]=y;
n_pond[y]+=n_pond[x];
n_pond[x]=0;
}
}*/
int main()
{
int T;
int n,m,j,i,k,x,y;
cin>>T;
while(T--)
{
cin>>n>>m;
memset(pond_npipe,0,sizeof(pond_npipe));
// memset(set,-1,sizeof(set));
memset(poi,0,sizeof(poi));
for(i=1; i<=n; ++i)
{
// n_pond[i]=1;
node[i].clear();
scanf("%d",&val[i]);
}
for(i=0; i<m; ++i)
{
scanf("%d%d",&x,&y);
node[x].push_back(y);
node[y].push_back(x);
pond_npipe[x]++;
pond_npipe[y]++;
// set_join(x,y);
}
for(i=1; i<=n; ++i)
{
if(pond_npipe[i]==1)
{
one_pipe.push(i);
poi[i]=1;
}
else if(pond_npipe[i]==0)
poi[i]=1;
}
int sum=one_pipe.size();
while(sum)
{
k=one_pipe.front();
one_pipe.pop();
sum--;
for(i=0; i<node[k].size(); ++i)
{
if(!poi[ node[k][i] ])
{
pond_npipe[ node[k][i] ]--;
if(pond_npipe[ node[k][i] ]<=1)
{
one_pipe.push(node[k][i]);
++sum;
poi[node[k][i]]=1;
//pond_npipe[ node[k][i] ]--;
}
}
}
}
long long sum_max=0;
sum_val=0;
for(i=1; i<=n; ++i)
{
if(!poi[i])
{
sum_num=1;
sum_val=val[i];
poi[i]=1;
dfs(i);
if(sum_num!=1 && sum_num%2==1)
{
sum_max+=sum_val;
}
}
}
cout<<sum_max<<endl;
}
return 0;
}