PondsTime Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 838 Accepted Submission(s): 287
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
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.
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
Sample Output
|
题意:有N个池塘和M条连接池塘的无向管道,每个池塘都有一个价值。现在要把临近池塘数小于2的池塘全部移除(不移除管道),让你求出所有由奇数个池塘组成的连通分支,并统计它们的价值和。
一开始以为是BCC,结果写了好几次都WA。最后想着用DFS跑一次,没想到过了。。。
思路:
1,先拓扑一下(注意只有度数为1的点才能入队);
2,枚举所有未标记的点,在原图跑一次DFS,统计出它所在连通分支里面的点数num和总权值sum(注意只有度数大于1的才累加),并标记该连通分支里面的所有点;
3,若num & 1,累加即可,最后的累加结果就是答案。
AC代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#define MAXN 10000+10
#define MAXM 200000+10
#define INF 10000000
#define LL long long
using namespace std;
struct Edge
{
int from, to, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int n, m;
int val[MAXN];//记录点权
void init()
{
edgenum = 0;
memset(head, -1, sizeof(head));
}
int d[MAXN];//度数
int vis[MAXN];
void toposort()
{
memset(vis, 0, sizeof(vis));
queue<int> Q;
for(int i = 1; i <= n; i++) if(d[i] == 1) vis[i] = 1, Q.push(i);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
int rec;//记录边编号
for(int i = head[u]; i != -1; i = edge[i].next)
{
if(--d[edge[i].to] == 1)
{
vis[edge[i].to] = 1;
Q.push(edge[i].to);
}
}
}
}
void addEdge(int u, int v)
{
Edge E = {u, v, head[u]};
edge[edgenum] = E;
head[u] = edgenum++;
}
void getMap()//建图
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
scanf("%d", &val[i]);
int a, b;
memset(d, 0, sizeof(d));
memset(vis, false, sizeof(vis));
while(m--)
{
scanf("%d%d", &a, &b);
d[a]++, d[b]++;
addEdge(a, b);
addEdge(b, a);
}
toposort();
}
int num;
bool used[MAXN];//标记
LL sum;
void DFS(int u)
{
used[u] = true;
if(d[u] >= 2)
{
sum += val[u];
num++;
}
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(!used[v])
DFS(v);
}
}
void solve()
{
memset(used, false, sizeof(used));
LL ans = 0;
for(int i = 1; i <= n; i++)
{
num = sum = 0;
if(used[i] == true) continue;//已经标记过
DFS(i);
if(num & 1)
ans += sum;
}
printf("%lld\n", ans);
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
init();
getMap();
solve();
}
return 0;
}