美文网首页
F - The Suspects(2017-01-16)

F - The Suspects(2017-01-16)

作者: 陌路晨曦 | 来源:发表于2017-01-18 02:16 被阅读0次

题目大意
这道题是一道并查集的题,大概意思就是编号为0的人被认为带有SARS病毒,与他同一组的人有可能被传染,而他们被传染后可能再次传染和他们同组的人;所以题目要求求出有多少人可能被传染?
思路
将输入的同组的人连起来,注意连的时候要把节点少的往节点大的树上连,不然可能会退化成链表;然后再搜索和0同一棵树上的人有多少;

代码如下:

#include<stdio.h>
int pre[30005];
int n,m,k,a,b;
int find(int x)
{
    if(pre[x]==x) return x;
    else
    return find(pre[x]);
}
int Union(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fy>fx)
    {
        pre[fy]=fx;
    }
    if(fy<fx)
    {
        pre[fx]=fy;
    }
}
void ini()
{
    for(int i=0;i<n;i++)
    {
        pre[i]=i;
    }
}
int main()
{
    
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        ini();
        for(int i=0;i<m;i++)
        {
            scanf("%d",&k);
            scanf("%d",&a);
            for(int j=1;j<k;j++)
            {
                scanf("%d",&b);
                Union(a,b);
            }
        }
        int ans=0;
        for(int i=0;i<n;i++)
        {
            if(find(i)==0)
            {
                ans++;
            }
        }
        printf("%d\n",ans);
    }
}

相关文章

  • F - The Suspects(2017-01-16)

    题目大意这道题是一道并查集的题,大概意思就是编号为0的人被认为带有SARS病毒,与他同一组的人有可能被传染,而他们...

  • 再贴一次并查集

    F - The Suspects POJ - 1611

  • Volunteers

    They were not real suspects but a couple of students who ...

  • 飞鸟集每日一品(189)

    泰戈尔原文: The pet dog suspects the universe for scheming to ...

  • POJ 1611

    The Suspects Severe acute respiratory syndrome (SARS), an...

  • 12

    The pet dog suspects the universe for scheming to take it...

  • POJ 1611 - The Suspects

    POJ 1611 - The Suspects Severe acute respiratory syndrome...

  • POJ——1611 The Suspects

    题目大意 这题将的就是有n个人,被分成m组(每个人可能在几个组里面),其中0号人物肯定是患有非典。而与0号同组的人...

  • The Suspects POJ - 1611

    Severe acute respiratory syndrome (SARS), an atypical pne...

  • 《菜根谈》163

    信人示己之诚,疑人显己之诈 (Believe show their sincerity, suspects sho...

网友评论

      本文标题:F - The Suspects(2017-01-16)

      本文链接:https://www.haomeiwen.com/subject/uhuvbttx.html