Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1026 Accepted Submission(s): 344
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
Input
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
Source
2015 ACM/ICPC Asia Regional Changchun Online
//分析:题目求尽可能的不断删掉边数小于2的点 最后求所有独立子块 如果点数为奇数则加起来 否则不加
//英语差真可怕 刚开始题意错了 直接跪了... 其实不难 首先不断求出边数小于2的点 删掉 并将连接的点减1 如果减后小于2 则进队 下次删掉 最后dfs求连通块
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
using namespace std;
const int N=10000+5;
vector<int > ve[N]; //邻接表
bool book[N]; //标记删了没
bool flag[N]; //用于求连通dfs标记用 不用回朔
int edge[N]; //点的边数
long long num[N],sum;
int n,m;
queue<int> q;
int child; //用于记录每个联通块的点数
inline void del(){
int i;
for(i=1;i<=n;i++)
if(edge[i]<2){
q.push(i);
book[i]=1;
}
while(!q.empty()){
int now=q.front();
q.pop();
for(i=0;i<ve[now].size();i++){
int x=ve[now][i];
if(book[x]==1) //删掉了的
continue;
edge[x]--;
if(edge[x]<2){
book[x]=1;
q.push(x);
}
}
}
}
void dfs(int now){
int i;
for(i=0;i<ve[now].size();i++){
int x=ve[now][i];
if(book[x]==1||flag[x]==1)
continue;
child++;
sum+=num[x];
flag[x]=1;
dfs(x);
}
}
int main(){
int t,i,a,b;
long long re;
scanf("%d",&t);
while(t--){
re=0;
scanf("%d%d",&n,&m);
// while(!q.empty()) q.pop();
for(i=1;i<=n;i++)
{
scanf("%lld",&num[i]);
book[i]=0;
flag[i]=0;
edge[i]=0;
ve[i].clear();
}
for(i=0;i<m;i++){
scanf("%d%d",&a,&b);
if(a==b) continue;
ve[a].push_back(b);
ve[b].push_back(a);
edge[a]++;
edge[b]++;
}
del();
for(i=1;i<=n;i++){
if(book[i]==0&&flag[i]!=1){ //还没删
flag[i]=1;
sum=num[i];
child=1;
dfs(i);
if(child%2) re+=sum;
}
}
printf("%lld\n",re);
}
return 0;
}