1107 Social Clusters[并查集][难]

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 find all the clusters.

Input Specification:

Each input file contains one test case. For each test case, the first line contains a positive integer N (1000), the total number of people in a social network. Hence the people are numbered from 1 to N. Then N lines follow, each gives the hobby list of a person in the format:

Ki​​: hi​​[1] hi​​[2] ... hi​​[Ki​​]

where Ki​​ (>0) is the number of hobbies, and hi​​[j] is the index of the j-th hobby, which is an integer in [1, 1000].

Output Specification:

For each case, print in one line the total number of clusters in the network. Then in the second line, print the numbers of people in the clusters in non-increasing order. The numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

8
3: 2 7 10
1: 4
2: 5 3
1: 4
1: 3
1: 4
4: 6 8 1 5
1: 4

Sample Output:

3
4 3 1

 题目大意:根据输入将用户分堆,也就是并查集的题目;输入格式:一共几个人,第几个人一共有几个爱好:爱好序号。根据兴趣爱好进行分堆,看哪些人在一起了,并按人数非升序排列。

//题目中给的样例共有3个簇,最大的4个人包括 2,4,6,8四个人,3个人的包括3,5,7三个人,这种没有重复,但是有没有可能会重复呢?

//我能想到的就是创建1000个向量,每个用户分别push,然后求出来长度!=1的向量,然后按照长度大小输出,其他没什么想法了。。。

代码来自:https://www.liuchuo.net/archives/2183

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> father, isRoot;
int cmp1(int a, int b){return a > b;}
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;
}
int main() {
    int n, k, t, cnt = 0;
    int course[1001] = {0};
    scanf("%d", &n);
    father.resize(n + 1);
    isRoot.resize(n + 1);
    for(int i = 1; i <= n; i++)
        father[i] = i;
    for(int i = 1; i <= n; i++) {
        scanf("%d:", &k);
        for(int j = 0; j < k; j++) {
            scanf("%d", &t);
            if(course[t] == 0)
                course[t] = i;//也就是将这个课程归并为i所有。
            Union(i, findFather(course[t]));//找到这个课程的父亲,因为两者有相同的父亲,
            //此处应该findFather(course[t])是父亲的,因为其是course的编号。
            //是一个簇内的,那么合并即可。
        }
    }
    for(int i = 1; i <= n; i++)
        isRoot[findFather(i)]++;//那些是根节点的都++;最终求出来的是簇内所含的人数。
    for(int i = 1; i <= n; i++) {
        if(isRoot[i] != 0) cnt++;
    }
    printf("%d\n", cnt);
    sort(isRoot.begin(), isRoot.end(), cmp1);
    for(int i = 0; i < cnt; i++) {
        printf("%d", isRoot[i]);
        if(i != cnt - 1) printf(" ");
    }
    return 0;
}

 

//真的太神奇了。

1.couser[i]表示爱好i第一次出现时的用户编号,如果=0表示这个爱好没出现。

2.如果新来了一个人k爱好也为i,那么将couser[i]和Kunion即可。

3.复习了并查集,并查集三要素:findFather+union+初始化father数组为自己;

4.这里使用了一个for循环遍历来确定每个簇有多少个人。

posted @ 2018-08-28 21:44  lypbendlf  阅读(202)  评论(0编辑  收藏  举报