Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2277 Accepted Submission(s): 720
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个池塘和M条连接池塘的无向管道,每个池塘都有一个价值。现在要把临近池塘数小于2的池塘全部移除(不移除管道),让你求出所有由奇数个池塘组成的连通分支,并统计它们的价值和。
解析:
不停的删掉度数小于等于1的点并更新各点度数,把删掉的点标记为1,直至无法删点,然后dfs,记录每个连通分支中点的个数,如果个数是奇数,累加即可。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 11000
#define maxm 2200000
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int head[maxn], cnt;
struct node {
int u, v, next;
};
node edge[maxm];
int n, m;
int val[maxn];
int du[maxn];
int vis[maxn];
void init(){
cnt = 0;
memset(head, -1, sizeof(head));
memset(du, 0, sizeof(du));
}
void add(int u, int v){
edge[cnt] = {u, v, head[u]};
head[u] = cnt++;
}
void topsort(){
queue<int>q;
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; ++i)
if(du[i] <= 1)
q.push(i);
while(!q.empty()){
int u = q.front(); q.pop();
vis[u] = 1;
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].v;
if(!vis[v] && --du[v] <= 1)
q.push(v);
}
}
}
ll len, sum;
void dfs(int u){
vis[u] = 1;
len++;
sum += val[u];
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].v;
if(!vis[v])
dfs(v);
}
}
int main (){
int T;
scanf("%d", &T);
while(T--){
init();
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i)
scanf("%d", &val[i]);
while(m--){
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
add(b, a);
du[a]++; du[b]++;
}
topsort();
ll ans = 0;
for(int i = 1; i <= n; ++i){
if(!vis[i]){
len = sum = 0;
dfs(i);<span style="display: none; width: 0px; height: 0px;" id="transmark"></span>
if(len & 1 && len > 2) ans += sum;
}
}
printf("%I64d\n", ans);
}
return 0;
}