HDU 543Ponds
2015-09-17 00:18
274 查看
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1902 Accepted Submission(s): 602
[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
题意:有N个池塘和M条连接池塘的无向管道,每个池塘都有一个价值。现在要把临近池塘数小于2的池塘全部移除(不移除管道),让你求出所有由奇数个池塘组成的连通分支,并统计它们的价值和。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 10010
#define MAXM 200010
#define INF 0x3f3f3f3f
struct node
{
int u,v,next;
} edge[MAXM];
int value[MAXN]; ///池塘价值
int degree[MAXN]; ///每个点的度
int flag[MAXN];
int vis[MAXN];
int head[MAXN],edgenum;
bool used[MAXN];
int n,m;
void addEdge(int u,int v) //前向星
{
edge[edgenum].u=u;
edge[edgenum].v=v;
edge[edgenum].next=head[u];
head[u]=edgenum++;
}
int finds(int u)
{
int r=u;
while(flag[r]!=r)
{
r=flag[r];
}
int s=u,t;
while(flag[s]!=r)
{
t=flag[s];
flag[s]=r;
s=t;
}
return r;
}
void toposort()
{
int i;
memset(vis,0,sizeof(vis));
queue<int>q;
for(i=1; i<=n; i++)
{
if(degree[i]<=1)
{
vis[i]=1;
if(degree[i]==1)
q.push(i);
}
}
while(!q.empty())
{
int u=q.front();//取队首元素
q.pop();
for(i=head[u]; i!=-1; i=edge[i].next)
{
degree[edge[i].v]--;
if(degree[edge[i].v]<=1)
{
vis[edge[i].v]=1;
if(degree[edge[i].v]==1)
q.push(edge[i].v);
}
}
}
}
void findsum()
{
int i,j;
long long int ans=0;
for(i=1; i<=n; i++)
{
if(vis[i]) continue;
int s=flag[i];
int num=0;
long long int sum=0;
for(j=1; j<=n; j++)
{
if(flag[j]==s&&!vis[j])
{
num++;
sum+=value[j];
vis[j]=1;
}
}
if(num%2)
{
ans+=sum;
}
}
printf("%lld\n",ans);
}
int main()
{
int T;
int a,b,i;
scanf("%d",&T);
while(T--)
{
memset(head,-1,sizeof(head));
memset(flag,0,sizeof(flag));
memset(degree,0,sizeof(degree));
scanf("%d%d",&n,&m);
for(i=1; i<=n; i++)
{
scanf("%d",&value[i]);
flag[i]=i;
}
edgenum=0;
while(m--)
{
scanf("%d%d",&a,&b);
degree[a]++;
degree[b]++;
addEdge(a,b);
addEdge(b,a);
int fa=finds(a);
int fb=finds(b);
if(fa!=fb)
{
flag[fa]=fb;
}
}
for(i=1;i<=n;i++)
{
int c=finds(i);
}
toposort();
findsum();
}
return 0;
}
相关文章推荐
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU-1213-How Many Tables
- Longest Consecutive Sequence,Distinct Subsequences,Interleaving String,Scramble String
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- hdu-5385
- HDU 1622 Trees On The Level
- HDU 1063 Exponentiation
- SARS病毒传染 并查集