我是靠谱客的博主 顺心铃铛,这篇文章主要介绍poj 1161 The Suspects,现在分享给大家,希望可以做个参考。

题目链接: The Suspects

题目大意:给你一个n和m,代表有n个人,m个朋友团体,每个团体有k个人,朋友的朋友也是朋友,问最后跟0在一个朋友集合里面的人有多少个(包括0本身)

题目思路:并查集,直接连,连完之后判断就好
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>

using namespace std;

int n,m,k,a,b,pre[100005];

int Find(int x){
    if(x == pre[x]) return x;
    return pre[x] = Find(pre[x]);
}

void join(int x,int y){
    int p = Find(x);
    int q = Find(y);
    if(p != q) pre[q] = p;
}

int main(){
    while(~scanf("%d%d",&n,&m)&&n+m){
        for(int i = 0;i < n;i++)
            pre[i] = i;
        while(m--){
            scanf("%d",&k);
            scanf("%d",&a);
            for(int i = 1;i < k;i++)
                scanf("%d",&b),join(a,b);
        }
        int ans = 0;
        for(int i = 0;i < n;i++){
            if(Find(i) == Find(0))
                ans++;
        }
        printf("%dn",ans);
    }
    return 0;
}

最后

以上就是顺心铃铛最近收集整理的关于poj 1161 The Suspects的全部内容,更多相关poj内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(129)

评论列表共有 0 条评论

立即
投稿
返回
顶部