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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VR(Virtual Reality)即虚拟现实,是一种可以创建和体验虚拟世界的计算机技术。它利用计算机生成一种模拟环境,是一种多源信息融合的、交互式的三维动态视景和实体行为的系统仿,使用户沉浸到该环境中。VR技术通过模拟人的视觉、听觉、触觉等感觉器官功能,使人能够沉浸在计算机生成的虚拟境界中,并能够通过语言、手势等自然的方式与之进行实时交互,创建了一种适人化的多维信息空间。 VR技术具有以下主要特点: 沉浸感:用户感到作为主角存在于模拟环境中的实程度。理想的模拟环境应该使用户难以分辨假,使用户全身心地投入到计算机创建的三维虚拟环境中,该环境中的一切看上去是的,听上去是的,动起来是的,甚至闻起来、尝起来等一切感觉都是的,如同在现实世界中的感觉一样。 交互性:用户对模拟环境内物体的可操作程度和从环境得到反馈的自然程度(包括实时性)。例如,用户可以用手去直接抓取模拟环境中虚拟的物体,这时手有握着东西的感觉,并可以感觉物体的重量,视野中被抓的物体也能立刻随着手的移动而移动。 构想性:也称想象性,指用户沉浸在多维信息空间中,依靠自己的感知和认知能力获取知识,发挥主观能动性,寻求解答,形成新的概念。此概念不仅是指观念上或语言上的创意,而且可以是指对某些客观存在事物的创造性设想和安排。 VR技术可以应用于各个领域,如游戏、娱乐、教育、医疗、军事、房地产、工业仿等。随着VR技术的不断发展,它正在改变人们的生活和工作方式,为人们带来全新的体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值