并查集——A1107.Social Clusters(30)

#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
const int N = 1010;
int father[N];
int isRoot[N] = {0};
int course[N] = {0};
int findFather(int x){
    int a = x;
    while(x != father[x]){
        x = father[x];
    }
    //路径压缩
    while(a != father[a]){
        int z = a;
        a = father[a];
        father[z] = x;
    }
    return x;
}
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;
        isRoot[i] = false;
    }
}
bool cmp(int a,int b){
    return a > b;
}
int main(){
    int n,k,h;
    scanf("%d",&n);
    init(n);
    for(int i=1;i<=n;++i){
        scanf("%d",&k);
        for(int j = 0;j<k;++j){
            scanf("%d",&h);
            if(course[h] == 0){
                course[h] = i;
            }
            Union(i,findFather(course[h]));
        }
    }
    for(int i=1;i<=n;++i){
        isRoot[findFather(i)]++;
    }
    int ans = 0;
    for(int i=1;i<=n;++i){
        if(isRoot[i] != 0){
            ans++;
        }
    }
    printf("%d\n",ans);
    sort(isRoot+1,isRoot+n+1,cmp);
    for(int i=1;i<=ans;++i){
        printf("%d",isRoot[i]);
        if(i < ans){
            printf(" ");
        }
    }

    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/JasonPeng1/p/12247193.html

时间: 2024-11-08 06:26:23

并查集——A1107.Social Clusters(30)的相关文章

A1107 Social Clusters (30分)

一.技术总结 这是一道并查集的题目,这里无非就是明白题目的含义,然后将套路用上基本上问题不大. 一个是father数组用于存储父亲结点,isRoot用于存储根结点,如果题目需要,还可以建立一个course数组,用于存储,例如这个题目就是用于判断该该兴趣是否有人涉及. findFather函数,用于查找结点的父亲结点,同时包含压缩路径. Union函数,用于合并. 初始化函数. 二.参考代码 #include<bits/stdc++.h> using namespace std; const i

PAT-1107 Social Clusters (30 分) 并查集模板

1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of their hobbies in common. Y

PAT Advanced 1107 Social Clusters (30) [并查集]

题目 When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A "social cluster" is a set of people who have some of their hobbies in common. You are supposed to

1107. Social Clusters (30)

When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A "social cluster" is a set of people who have some of their hobbies in common. You are supposed to fi

PAT_A1107#Social Clusters

Source: PAT A1107 Social Clusters (30 分) Description: When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of

1034. Head of a Gang (30) -string离散化 -map应用 -并查集

题目例如以下: One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone call

PAT A1107——并查集

Social Clusters When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of their hobbies in common. You are suppo

PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls mad

1053 Path of Equal Weight (30分)(并查集)

Given a non-empty tree with root R, and with weight W?i?? assigned to each tree node T?i??. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L. Now given any weighted tre