首页 > 其他 > 详细

POJ 1611 The Suspects

时间:2014-07-06 09:36:44      阅读:308      评论:0      收藏:0      [点我收藏+]

并查集问题。


题意是说: 编号0 可能感染SARS。

然后有M个 团队,团队的人可能互相感染。

找出所有可能感染的SARS人。


把某个团队的并起来就好了。最后扫描一遍。

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
using namespace std;
int n,m;
int fa[30001];
int father(int x)
{
    if(x!=fa[x])
        return fa[x]=father(fa[x]);
}
int main()
{
    while(scanf("%d%d",&n,&m),n||m)
    {
        for(int i=0;i<=n;i++)
            fa[i]=i;
        while(m--)
        {
            int t,u,v;
            scanf("%d",&t);
            if(!t)continue;
            scanf("%d",&u);
            u=father(u);
            while(--t)
            {
                scanf("%d",&v);
                v=father(v);
                if(u==v)continue;
                fa[v]=u;
            }
        }
        int ans=0;
        int s=father(0);
        for(int i=0;i<=n;i++)
            if(father(i)==s)ans++;
        printf("%d\n",ans);
    }
}


POJ 1611 The Suspects,布布扣,bubuko.com

POJ 1611 The Suspects

原文:http://blog.csdn.net/dongshimou/article/details/37049799

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!