PAT1107 Social Clusters

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1005
using namespace std;
int father[N];
int isRoot[N];
int course[N];
int findFather(int x){
    if(father[x] == x) return x;
    else{
        int tp = findFather(father[x]);
        father[x] = tp;
        return tp;
    }
}
void Union(int a,int b){
    int faA = findFather(a);
    int faB = findFather(b);
    if(faA != faB)
        father[faA] = faB;
}
void init(int n){
    for(int i = 1;i <= n;i++){
        father[i] = i;
        course[i] = 0;
        isRoot[i] = 0;
    }
}

bool cmp(int a,int b){
    return a > b;
}

int main(){
    int n;
    scanf("%d",&n);
    init(n);
    for(int i = 1;i <= n;i++){
        int k,h;
        scanf("%d:",&k);
        while(k--){
            scanf("%d",&h);
            if(course[h] == 0){
                course[h] = i;
            }
            Union(i,findFather(course[h]));
        }   
    }
    int ans = 0;
    for(int i = 1;i <= n; i++){
        isRoot[findFather(i)]++;
    }
    for(int i = 1;i <= n; i++){
        if(isRoot[i] != 0)
        ans++;
    }
    sort(isRoot,isRoot+n+1,cmp);    
    printf("%d\n",ans);
    for(int i = 0;i < ans; i++){
        if(isRoot[i]){
            printf(i==0?"%d":" %d",isRoot[i]);
        }

    }
    return 0;
} 
全部评论

相关推荐

07-18 10:39
门头沟学院 Java
点赞 评论 收藏
分享
06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
实习吐槽大会
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务