Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3558 Accepted Submission(s): 1063
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
大体题意:
告诉你n 个池塘,有些池塘是想连接的,他要删除一些池塘,删除原则是这个池塘连接的其他池塘必须小于2个,直到不能删为止,问最后连通池塘是奇数的连通的权值之和!
思路:
用个数组记录连接的个数,用dfs删除点,在用并查集求连通个数,遍历连通块求出是奇数的权值之和即可!
详细见代码:
/************************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ ┏━┛ ┆
┆ ┃ ┃ ┆
┆ ┃ ┗━━━┓ ┆
┆ ┃ AC代马 ┣┓┆
┆ ┃ ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstdlib>
#include<stack>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#define fout freopen("out.txt","w","stdout");
using namespace std;
const int maxn = 10000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int mod = 1e9+7;
typedef long long ll;
typedef unsigned long long llu;
int gcd(int a,int b){ return !b?a:gcd(b,a%b); }
ll my_pow(ll a,ll n){
ll ans = 1;
while(n){
if (n & 1)
ans = (ans % mod * a % mod) % mod;
n/=2;
a = (a%mod*a%mod)%mod;
}
return ans;
}
vector<int>g[maxn];
vector<pair<int,int> >bing[maxn];
int n,m;
ll sum = 0ll;
int vis[maxn];
int a[maxn];
int lenn[maxn];
int fa[maxn];
void dfs(int k){
sum -= a[k];
vis[k] = 1;
//lenn[k]--;
// cout << k << endl;
int len = g[k].size();
for (int i = 0; i <len; ++i){
int u = g[k][i];
if (vis[u])continue;
lenn[u]--;
if (lenn[u] <= 1)dfs(u);
}
}
int find(int x){
return fa[x] == x ? x : find(fa[x]);
}
void add(int x,int y){
int xx = find(x);
int yy = find(y);
if (xx != yy){
if (xx > yy)swap(xx,yy);
fa[yy] = xx;
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
for (int i = 1; i <= n; ++i){
g[i].clear();
vis[i] = 0;
fa[i] = i;
bing[i].clear();
}
memset(lenn,0,sizeof lenn);
// memset(vis,0,sizeof vis);
sum = 0ll;
for (int i = 1; i <= n; ++i)scanf("%d",&a[i]),sum += a[i];
// cout << sum<<endl;
for (int i = 0; i < m; ++i){
int u,v;
scanf("%d %d",&u,&v);
add(u,v);
g[u].push_back(v);
g[v].push_back(u);
lenn[u]++;
lenn[v]++;
}
// cout << "hehe" << lenn[2] << endl;
for (int i = 1; i <= n; ++i){
if (lenn[i] <= 1 && !vis[i]){
dfs(i);
i = 0;
}
}
sum = 0ll;
for (int i = 1; i <= n; ++i){
if (vis[i])continue;
int faa = find(i);
// cout << faa << endl;
bing[faa].push_back(make_pair(i,a[i]));
}
for (int i = 1; i <= n; ++i){
int len = bing[i].size();
if (len > 0 && len % 2 == 1){
for (int j = 0; j < len; ++j){
sum += bing[i][j].second;
}
// sum += a[i];
}
}
// cout << bing[1].size() << endl;
printf("%I64d\n",sum);
}
return 0;
}
/*
3 2
1 2 3
1 2
2 3
*/