hdu 5438 Ponds 拓扑排序

Ponds

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1001&cid=621

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

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

HINT

题意

给你一个图,然后要求把度数小于2的点全部删去,然后问你奇数集合的点权和是多少

注意,你删去了一个点之后,可能会使得一些点又变成了度数小于2的

题解:

用类似拓扑排序的思想去做就ok啦

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; const int N=;
long long a[N],ans;
int n,m,T,cnt,ok[N],vis[N],pre[N],nxt[N],to[N],tot[N],col;
vector<int> s[N];
queue<int> q; void dfs(int x,int fa)
{
s[col].push_back(x);
ok[x]=;
for(int p=pre[x];p!=-;p=nxt[p])
{
if((!vis[p])||(!ok[to[p]])) continue;
if(p==(fa^)) continue;
dfs(to[p],p);
}
}
void makeedge(int x,int y)
{
to[cnt]=y;nxt[cnt]=pre[x];pre[x]=cnt++;
to[cnt]=x;nxt[cnt]=pre[y];pre[y]=cnt++;
} int main()
{
scanf("%d",&T);
while(T--)
{
memset(tot,,sizeof(tot));
memset(pre,-,sizeof(pre));
memset(ok,,sizeof(ok));
memset(vis,,sizeof(vis));
ans=0LL;cnt=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
for(int i=;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
tot[x]++;tot[y]++;
makeedge(x,y);
}
while(!q.empty()) q.pop();
for(int i=;i<=n;i++)
{
if(tot[i]<)
{
q.push(i);
}
}
while(!q.empty())
{
int x=q.front();
q.pop();
ok[x]=;
for(int p=pre[x];p!=-;p=nxt[p])
{
vis[p]=;
tot[x]--;
tot[to[p]]--;
if(ok[to[p]]&&tot[to[p]]<)
{
q.push(to[p]);
}
}
}
col=;
for(int i=;i<=n;i++)
{
col++;
s[col].clear();
if(ok[i])
{
dfs(i,cnt+);
if(s[col].size()%==)
{
for(int j=;j<s[col].size();j++)
{
ans+=a[s[col][j]];
}
}
}
}
printf("%I64d\n",ans);
}
return ;
}
上一篇:SAP Spartacus MyCompany菜单里Org unit的Add按钮Accessibility问题初始分析


下一篇:Html5 学习系列(五)Canvas绘图API快速入门(2)