您的位置:首页 > 其它

HDU 5438 拓扑排序+DFS

2016-07-13 19:46 204 查看

Ponds

Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3234 Accepted Submission(s): 997


[align=left]Problem Description[/align]
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

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

1
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7

[align=left]Sample Output[/align]

21

题意:一直去度数小于1的点,直到不能再去。

题解:对图拓扑排序后,剩余部分DFS判断是否为奇数个,奇数个直接加权值。 坑点在下面标出。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include <vector>
using namespace std;
const int maxn = 1e4+5;
vector<int> g[maxn];
int v[maxn];
int ans[maxn];
int vis[maxn];
int in[maxn];

int t;
int p,m;
int cnt;
void toposort()
{
queue<int> q;
for(int i = 1; i<=p; i++)
if(in[i] <= 1)   //还有度数为0的点
q.push(i);
while(!q.empty())
{
int temp = q.front();
q.pop();
ans[temp]++;
for(int j = 0; j<g[temp].size(); j++)
{
int vv = g[temp][j];
if(in[vv]<=1) continue;  //z这里有坑,否则就陷入死循环了。
in[vv]--;
if(in[vv] <= 1)
q.push(vv);
}
}
}
void dfs(int s,int& count,long long& sum)
{
if(ans[s]>0||vis[s]) return;
vis[s] = 1;
for(int i = 0; i<g[s].size(); i++)
{
int vv = g[s][i];
if(ans[vv] == 0&& !vis[vv])
{
count++;
sum += v[vv];
dfs(vv,count,sum);
}
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(ans,0,sizeof(ans));
memset(in,0,sizeof(in));
memset(vis,0,sizeof(vis));
scanf("%d %d",&p,&m);
for(int i = 1; i<=p; i++)
{
scanf("%d",&v[i]);
}
int l,r;
for(int i = 1; i<=p; i++) g[i].clear();
for(int i = 1; i<=m; i++)
{
scanf("%d %d",&l,&r);
g[l].push_back(r);
g[r].push_back(l);
in[l]++;
in[r]++;
}
toposort();
long long sum = 0,sum1 = 0;
int count = 0;
for(int i = 1; i<=p; i++)
{
if(ans[i] == 0&&!vis[i])
{
sum1 = v[i];
count = 1;
dfs(i,count,sum1);
if(count%2 == 1) sum += sum1;
}
}
printf("%I64d\n",sum);

}
return 0;
}
/*
312
7 10
1 20 300 400 500 1000 5000
1 2
1 3
1 4
2 3
2 4
3 4
5 6
6 7
5 7
3 6

2 1
10 20
1 2

3 2
10 100 1000
1 2
1 3

3 1
10 100 1000
1 2
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: