hdu 5438 Ponds(拓扑排序+并查集)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5438

Ponds

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


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(1T30) 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(1p104) which represents the number of ponds she owns, and the other is the number m(1m105) which represents the number of pipes.

The next line contains p numbers v1,...,vp , where vi(1vi108) 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
 
分析:由题意可以看出是拓扑排序的操作,至于最后没有删除的点的集合可以用并查集来统计元素的个数,满足条件的则计入结果。由于点的极限数量是很大的,所以用链式前向星来存储点的连接信息。注意怎样及时更新点个数和val数据。
如果思路是安全的,那么错误会很少,及时有没想到的部分,也能很快发觉并修补漏洞。开始自己想直接在拓扑排序中进行点的个数和Val的更新,然而不断WA,实在看不出错误,后来换成先拓扑排序再进行并查集的更新数据,但直接用链式前向星更新的话会出错的,因为其特殊的结构--一点联系多个点,如果用标记法,有可能出现多,少的统计。其实越简单的思路安全性越高,直接用两个数组把边的端点信息存储下来即可。另外,自己因为长整形数据输入格式写成了%d Wa了半天。。仔细啊,不要给错误生成任何的可乘之机,哪怕是浪费空间,时间!
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e5+10;
typedef long long ll;
struct edge
{
    int v,next;
}edge[maxn*2];
int t,head[maxn];
int indeg[maxn],vis[maxn];
int x[maxn],y[maxn],father[maxn],sum[maxn];
ll val[maxn];
void inti()
{
    t=0;
    memset(head,-1,sizeof(head));
    memset(indeg,0,sizeof(indeg));
    memset(vis,0,sizeof(vis));
}
void add(int u,int v)
{
    edge[t].v=v;
    edge[t].next=head[u];
    head[u]=t++;
    indeg[v]++;
}
int que[maxn],b1,b2;
void tuopu(int n)
{
    b1=b2=0;
    for(int i=1;i<=n;i++){
        if(indeg[i]<=1)
        {
            vis[i]=1;
            que[b2++]=i;
        }
    }
    while(b1!=b2){
        int temp=que[b1++];
        for(int i=head[temp];i>-1;i=edge[i].next)
        {
            int to=edge[i].v;
            if(vis[to]==0)
            {
                indeg[to]--;
                if(indeg[to]==1)
                {
                    que[b2++]=to;
                    vis[to]=1;
                }
            }
        }
    }
}
int find(int x)
{
    if(x==father[x]) return x;
    return father[x]=find(father[x]);
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        inti();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&val[i]);
            father[i]=i;
            sum[i]=1;
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            add(x[i],y[i]);
            add(y[i],x[i]);
        }
        tuopu(n);
        for(int i=0;i<m;i++)
        {
            if(vis[x[i]]==0&&vis[y[i]]==0)
            {
                int fx=find(x[i]);
                int fy=find(y[i]);
                if(fx!=fy)
                {
                    father[fx]=fy;
                    val[fy]+=val[fx];
                    sum[fy]+=sum[fx];
                }
            }
        }
        ll res=0;
        for(int i=1;i<=n;i++){
            if(father[i]==i&&vis[i]==0&&sum[i]&1) res=res+val[i];
        }
        printf("%I64d\n",res);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值