Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 4878 Accepted Submission(s): 1397
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
#include<stdio.h>
#include<iostream>
#include<vector>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 1e4+5;
bool flag[maxn];
int n,m,num,val[maxn],du[maxn];
long long ans;
vector<int> v[maxn];
void top()
{
queue<int> q;
for(int i=1; i<=n; i++)
if(du[i]==1||du[i]==0)
q.push(i);
while(!q.empty())
{
int x=q.front();
q.pop();
flag[x]=1;
for(int i=0;i<v[x].size();i++)
{
int xx=v[x][i];
if(!flag[xx])
{
du[xx]--;
if(du[xx]==1||du[xx]==0)
{
q.push(xx);
}
}
}
}
}
void bfs(int s)
{
queue<int> q;
q.push(s);
flag[s]=1;
num++;
ans+=val[s];
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=0; i<v[x].size(); i++)
{
int xx=v[x][i];
if(!flag[xx])
{
ans+=val[xx];
num++;
flag[xx]=1;
q.push(xx);
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int t;
cin>>t;
while(t--)
{
for(int i=1; i<=n; i++)
v[i].clear();
memset(flag,0,sizeof(flag));
memset(du,0,sizeof(du));
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
scanf("%d",&val[i]);
int a,b;
for(int i=0; i<m; i++)
{
scanf("%d%d",&a,&b);
if(a==b) continue;
v[a].push_back(b);
v[b].push_back(a);
du[a]++;
du[b]++;
}
top();
long long sum=0;
for(int i=1; i<=n; i++)
{
num=0;
ans=0;
if(!flag[i]) bfs(i);
if(num%2) sum+=ans;
}
printf("%lld\n",sum);
}
return 0;
}
代码写的很乱。