PAT甲级真题 1107 Social Clusters (30分) C++实现(并查集:路径压缩+优先级)

题目

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 : h​i​​ [1] h​i[2] … h​i[Ki]
where K​i(>0) is the number of hobbies, and h​i​​ [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

思路

一开始直接使用了一组set记录所有朋友圈。对于输入的每个人,先判断其兴趣是否在之前的set中出现过,若出现过就加入该set,并检查剩下的元素是否出现在其它set里,有的话就将其它set与该set合并。这个思路很直观,但代码很丑陋:

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;

int main() {
    int n;
    scanf("%d", &n);
    vector<set<int> > ss;
    vector<int> nums;
    for (int i=0; i<n; i++){
        int m;
        scanf("%d: ", &m);
        vector<int> a(m);
        for (int j=0; j<m; j++){
            scanf("%d", &a[j]);
        }
        bool flag = false;
        for (int j=0; j<m; j++){
            for (int k=0; k<ss.size(); k++){  //第k个set
                if (ss[k].find(a[j])!=ss[k].end()){  //找到了
                    for (int l=0; l<m; l++){
                        if (l!=j) ss[k].insert(a[l]);
                    }
                    for (int l=j+1; l<m; l++){  //后序数字是否出现在其它set里
                        for (int k1=0; k1<ss.size(); k1++){
                            if (k1==k) continue;
                            if (ss[k1].find(a[l]) != ss[k1].end()){  //合并该set
                                for (auto it : ss[k1]){
                                    ss[k].insert(it);
                                }
                                ss[k1].clear();
                                nums[k] += nums[k1];
                                nums[k1] = 0;
                            }
                        }
                    }
                    nums[k]++;
                    flag = true;
                    break;
                }
            }
            if (flag) break;
        }
        if (!flag){  //未找到
            set<int> newSet; 
            for (int j=0; j<m; j++){
                newSet.insert(a[j]);
            }
            nums.push_back(1);
            ss.push_back(newSet);
        }
    }
    sort(nums.begin(), nums.end());
    int m = 0;
    while (m<nums.size() && nums[m]==0) m++;
    printf("%d\n%d", nums.size()-m, nums[nums.size()-1]);
    for (int i=nums.size()-2; i>=m; i--){
        printf(" %d", nums[i]);
    }
    return 0;
}

更简洁的做法是用并查集,为每个集合选出代表,判断元素是否相交只需比较二者的代表是否相同。用到了路径压缩和记录优先级来提高效率。

代码

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int hobbies[1001],father[1001],level[1001],isRoot[1001];

void init() {
    for(int i=1;i<1001;i++){
        father[i] = i;
        level[i] = 0;
    }
}
//路径压缩,即将返回值赋给了father[x]
int findFather(int x) {
    return x == father[x] ? x : (father[x] = findFather(father[x]));
}

void Union(int a,int b) {
    int fa=findFather(a);
    int fb=findFather(b);
    if (level[fa] <= level[fb])  //优先级低的合并到优先级高的树根
        father[fa] = fb;
    else
        father[fb] = fa;
    if(level[fa]==level[fb] && fa!=fb)  //fa与fb优先级相同时,fa以fb为根,所以fb的级别又提高了1
        level[fb]++;
}

int main(){
    int n;
    scanf("%d",&n);
    init();
    for(int i=1;i<=n;i++){
        int k;
        scanf("%d:",&k);
        for(int j=0;j<k;j++){
            int t;
            scanf("%d",&t);
            if(hobbies[t]==0)
                hobbies[t]=i;//记录这是谁的爱好,以第一次出现该爱好的人为准
            Union(i,hobbies[t]);
        }
    }
    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++;
    }
    printf("%d\n",cnt);
    sort(isRoot,isRoot+n+1,greater<int>());
    printf("%d",isRoot[0]);
    for(int i=1;i<cnt;i++){
        printf(" %d",isRoot[i]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值