Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1699 Accepted Submission(s): 542
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
//先把入度小于2的边压人队列 与之相连的点的入度减1后在搜索满足联通快的个数为奇 累加
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
const int maxn=10000+10;
const int inf=(1<<30);
int a[maxn],indegree[maxn],n;
int vis[maxn],cnt=0;
struct Node
{
int v;
int nex;
}Edge[400000+10];
int nex[maxn];
void add(int v,int u)
{
Edge[cnt].v=v;
Edge[cnt].nex=nex[u];
nex[u]=cnt++;
}
void bfs()
{
queue <int> q;
for(int i=1;i<=n;i++)
if(indegree[i]<2)
{
vis[i]=1;
indegree[i]--;
q.push(i);
}
while(!q.empty())
{
int a=q.front();
q.pop();
for(int i=nex[a];i!=-1;i=Edge[i].nex)
{
int t=Edge[i].v;
indegree[t]--;
if(indegree[t]==1)
{
q.push(t);
indegree[t]--;
vis[t]=1;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int p,v1,v2;
memset(indegree,0,sizeof(indegree));
memset(nex,-1,sizeof(nex));
memset(vis,0,sizeof(vis));
scanf("%d%d",&n,&p);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
cnt=0;
for(int i=1;i<=p;i++)
{
scanf("%d%d",&v1,&v2);
indegree[v1]++;
indegree[v2]++;
add(v1,v2);
add(v2,v1);
}
bfs();
queue <int> q;
__int64 res=0,ans=0;;
for(int i=1;i<=n;i++)
{
res=0;
if(!vis[i])
{
res+=a[i];
vis[i]=1;
q.push(i);
int cnt=1;
while(!q.empty())
{
int c=q.front();
q.pop();
for(int k=nex[c];k!=-1;k=Edge[k].nex)
{
if(!vis[Edge[k].v])
{
cnt++;
q.push(Edge[k].v);
vis[Edge[k].v]=1;
res+=a[Edge[k].v];
}
}
}
if(cnt%2==1)
ans+=res;
}
}
printf("%I64d\n",ans);
}
return 0;
}