PAT甲级刷题记录——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号喜欢5 3,7号喜欢6 8 1 5,因为都喜欢活动5,所以就被判在同一个圈子里)

题目要求的输出是:第一行是形成的圈子个数(也就是集合个数),第二行是每个集合的人数(从大到小排序)。

这题和平时见到的“裸”并查集有点不同,“裸”并查集一般都是直接告诉你,如果A和B是朋友,B和C是朋友,那么A和C也是朋友,这样子的话,每次在输入的时候,直接union就行了,这题呢需要通过“共同爱好”这个中间桥梁,来把好朋友之间串联起来,因此,我们就可以用一个数组hobby[tmpid]来记录喜欢活动tmpid的人的编号。于是,就有以下两种情况:

①hobby[tmpid]的值为0,说明之前并没有人喜欢这个tmpid的活动,那么就把自己放进去:hobby[tmpid] = i;
②hobby[tmpid]的值不为0,说明之前有人喜欢这个活动(或者说已经有一个圈子的人都喜欢这个活动了),那么只要把自己合并过去即可:Union(i, hobby[tmpid])。

PS:注意,这里不知道为什么晴神和柳神写的题解都是Union(i, findFather(hobby[tmpid])),我个人认为,只要把自己和喜欢那个活动的人合并即可(这么写也是能AC的),因为Union函数的操作本身就是会寻找根结点的,而且这么写理解起来也要更加简单一点】

至于怎么从大到小输出就很简单了,直接对N个结点的isRoot数组从大到小排序(该数组记录的是每个集合下的元素个数),然后从1到cnt(cnt是集合个数)输出就行了(因为排完序之后是4 3 1 0 0 0…(后面还有若干个0),但是排完序之后下标对应的就不是根结点了哈,慎用排序)。

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 1001;
int hobby[maxn] = {0};//hobby[i]存储的是喜欢i号活动的人的编号
int father[maxn] = {0};
int isRoot[maxn] = {0};
bool cmp(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(){
    for(int i=0;i<maxn;i++) father[i] = i;
    int N;
    scanf("%d", &N);
    for(int i=1;i<=N;i++){
        int K;
        scanf("%d", &K);
        getchar();//接受冒号
        getchar();//接受空格
        for(int j=0;j<K;j++){
            int tmpid;
            scanf("%d", &tmpid);
            if(hobby[tmpid]==0) hobby[tmpid] = i;//没出现过,就把自己放进去
            else{
                Union(i, hobby[tmpid]);//如果之前有人喜欢,那么合并集合
            }
        }
    }
    for(int i=1;i<=N;i++){
        isRoot[findFather(i)]++;
    }
    int cnt = 0;
    for(int i=1;i<=N;i++){
        if(isRoot[i]!=0) cnt++;
    }
    sort(isRoot+1, isRoot+N+1, cmp);
    printf("%d\n", cnt);
    for(int i=1;i<=cnt;i++){
        if(i==1) printf("%d", isRoot[i]);
        else printf(" %d", isRoot[i]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值