Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3829 Accepted Submission(s): 1135
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
先用类似top排序的方法标记一下要被删掉得点
然后用并查集或者dfs求出每个环点得数量。判断是否是奇数。然后求个和就行。
#include <cmath>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;
#define MAXN 200010
#define LEN 200010
#define INF 1e9+7
#define MODE 1000000
#define pi acos(-1)
#define g 9.8
typedef long long ll;
vector <int> G[MAXN];
int degree[MAXN];
long long v[MAXN];
int vis[MAXN];
void dfs(int s){
vis[s]=1;
for(int i=0;i<G[s].size();i++){
int t=G[s][i];
if(!vis[t]){
degree[t]--;
if(degree[t]==0){
vis[t]=1;
}
if(degree[t]==1){
dfs(t);
}
}
}
}
int cnt=0;
long long res=0;
int used[MAXN];
void dfs2(int s){
used[s]=1;
res+=v[s];
cnt++;
for(int i=0;i<G[s].size();i++){
int t=G[s][i];
if(!vis[t]&&!used[t]){
dfs2(t);
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
int p,m;
memset(vis, 0, sizeof(vis));
memset(degree, 0, sizeof(degree));
memset(used, 0, sizeof(used));
long long sum=0;
scanf("%d%d",&p,&m);
for(int i=1;i<=p;i++){
scanf("%lld",v+i);
G[i].clear();
}
while(m--){
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
degree[a]++;
degree[b]++;
}
for(int i=1;i<=p;i++){
if(degree[i]==0&&!vis[i]){
vis[i]=1;
}
else if(degree[i]==1&&!vis[i]){
dfs(i);
}
}
for(int i=1;i<=p;i++){
if(!vis[i]){
cnt=0;
res=0;
dfs2(i);
if(cnt%2==1&&cnt>1){
sum+=res;
}
}
}
printf("%lld\n",sum);
}
}