ZOJ1789

题意:0号人被视为疑似病例,与它有间接或直接联系的人都被视为疑似,问有多少人是疑似病例

题解:并查集然后看和0同一集合的节点个数

#include<bits/stdc++.h>
using namespace std;
int n,m;
typedef pair<int,int> pii;
int father[31000];
int GetFather(int x)
{
    if (father[x]==x)
        return x;
    else
    {
        int t=GetFather(father[x]);
        father[x]=t;
        return t;
    }
}
void Gao()
{
    vector<pii>a;
    for (int i=0; i<n; i++)
        father[i]=i;
    for(int i=0; i<m; i++)
    {
        int x,y,t;
        cin>>t;
        if (t==0)
            continue;
        cin>>x;
        for (int j=2;j<=t;++j)
            scanf("%d",&y),a.push_back(make_pair(x,y));
    }
    for (int i=0; i<a.size(); i++)
    {
        int x1,x2;
        x1=GetFather(a[i].first);
        x2=GetFather(a[i].second);
        if (x1!=x2)
            father[x1]=x2;
    }
    int ans=0;
    for (int i=0;i<n;i++)
        if (GetFather(i)==GetFather(0))
            ans++;
    cout<<ans<<endl;
}
int main()
{
    while (cin>>n>>m &&n)
        Gao();
        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值