您的位置:首页 > 其它

HDU 5438 Ponds(拓扑排序 + DFS)

2015-09-19 09:55 288 查看

Ponds

[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

 

Sample Output

21

 

【思路分析】

   题意是有n个泳池,一共有m条通道将其中的泳池连接起来,现在需要拆除那些相连最多一个的泳池,求剩下连通块中泳池为奇数个的这些连通块的权值之和。抽象出来就是删去无向图中度为0或者1的结点,再求剩下的连通块中结点为奇数个连通块的权值。

   先用拓扑排序删去度为0或者1的结点,再用DFS对每个连通块的结点遍历计数即可。

代码如下:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxm = 1e5 + 5;
const int maxn = 1e4 + 5;
int n,m,cnt,num;
int head[maxn],deg[maxn];//deg为结点的度数
int u,v;
bool visited[maxn];
ll tmp,ans,val[maxn];
struct Edge
{
int to,next;
}edge[maxm];
queue <int> q;

void addEdge(int u,int v)
{

edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void init()
{
while(!q.empty())
{
q.pop();
}
cnt = ans = 0;
memset(head,-1,sizeof(head));
memset(deg,0,sizeof(deg));
memset(visited,false,sizeof(visited));

scanf("%d %d",&n,&m);
for(int i = 1;i <= n;i++)
{
scanf("%lld",&val[i]);
}
for(int i = 1;i <= m;i++)
{
scanf("%d %d",&u,&v);
addEdge(u,v);
addEdge(v,u);
deg[u]++;
deg[v]++;
}

}
void topoSort()//删点
{
for(int i = 1;i <= n;i++)
{
if(deg[i] == 0)
{
visited[i] = true;//直接删去独立结点
}
if(deg[i] == 1)//度为1的结点入队,因为删去该结点后其连接的结点度数可能变为0或1
{
visited[i] = true;
q.push(i);
}
}
while(!q.empty())
{
int temp = q.front();
q.pop();
for(int i = head[temp];i != -1;i = edge[i].next)
{
int v = edge[i].to;//temp相邻的结点
deg[v]--;
if(deg[v] == 0)
{
visited[v] = true;
}
if(deg[v] == 1)
{
visited[v] = true;
q.push(v);
}
}
}
}
void dfs(int u,int des)//计数遍历回环图,des记录终点(也就是一开始的起点)
{
visited[u] = true;
num++;
tmp += val[u];
for(int i = head[u];i != -1;i = edge[i].next)
{
int v = edge[i].to;
if(!visited[v] && v != des)
{
dfs(v,u);
}
}
return;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
topoSort();
for(int i = 1;i <= n;i++)
{
if(!visited[i])
{
num = 0;
tmp = 0;
dfs(i,0);
if(num & 1)
{
ans += tmp;
}
}
}
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: