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 分析:并查集的简单应用:解决关键是将人属于同一集合的合并起来,本题应该通过相同的爱好合并起来;
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std; vector<int> father(), num();
int cnt;//记录集合数 //sort()从大到小排序比较函数
bool cmp(int a, int b){
return a > b;
} //初始化,每个人属于不同集合
void init(int n){
for(int i = ; i <= n; i++){
father[i] = i;
}
} int GetParent(int x){
if(x == father[x])
return x;
father[x] = GetParent(father[x]);//路径压缩
return father[x];
} void merge(int x, int y){
int fx = GetParent(x);
int fy = GetParent(y);
if(fx != fy){
cnt--;//不属于同一集合,合并之后集合数减一
father[fy] = fx;
}
} int main(){
int n, k, h, i;
int hobby[] = {};
scanf("%d", &n); //调用初始化函数
init(n); cnt = n;//初始化集合数为n //处理输入数据,合并集合
for(i = ; i <= n; i++) {
scanf("%d:", &k);
for(int j = ; j < k; j++) {
scanf("%d", &h);
if(hobby[h] == )
hobby[h] = i;
merge(hobby[h], i);
}
}
for(i = ; i <= n; i++)
//这里一定要注意,要找到i所属集合的根节点,num加一,num[father[i]]++是有问题的,因为
//这棵树深度不一定是2.。。即father[i]不一定是i所属集合的根节点
num[GetParent(i)]++; printf("%d\n", cnt); sort(num.begin(), num.end(), cmp); printf("%d", num[]);
for(i = ; i < cnt ; i++)
printf(" %d", num[i]);
return ;
}
pat甲级 1107. Social Clusters (30)的更多相关文章
- PAT甲级1107. Social Clusters
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...
- PAT甲级——1107 Social Clusters (并查集)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30 ...
- [并查集] 1107. Social Clusters (30)
1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...
- 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)
题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...
- PAT (Advanced Level) 1107. Social Clusters (30)
简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- 1107 Social Clusters (30)(30 分)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- PAT甲级——A1107 Social Clusters
When register on a social network, you are always asked to specify your hobbies in order to find som ...
随机推荐
- P4110 [HEOI2015]小L的白日梦
传送门 题解 //minamoto #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef l ...
- Beyond Compare 激活解决办法
问题: 当你使用过一段时间后会提示有问题,需要激活或者什么. 解决办法: 找到这个路径并删除其下Beyond Compare 3文件夹即可正常使用. C:\Users\******\AppData\R ...
- Linux下安装网络软件的步骤
Linux下安装网络软件的步骤(给linux初学者,linux大神请绕路) 首先下载你所需要的软件带有deb后缀的文件 然后切换到该文件的目录 切换到超级用户权限或者是(sudo) 使用sudo dp ...
- MySql 同表复制数据 可以改变数据
Mysql语法: INSERT INTO 表名 (字段) SELECT 字段 FROM 表名 WHERE 条件: 如果要修改其中某一个字段,在查询语句中:x(要改变的值) as 字段名. eg: IN ...
- C# 写的正整数四则运算计算器
实际上没能做出来负数.括号.小数的功能,才写这么个标题 大神直接略过,也欢迎指指点点-.- 输入一个四则运算表达式,计算出结果,想想不难,实现起来也不是很容易的. 流程:1.for循环输入的四则运算字 ...
- python自动化--语言基础三字典、函数、全局/局部变量
字典 dict1 = {,'class':'first'} print(dict1.keys()) #打印所有的key值 print(dict1.values()) #打印所有的values值 pri ...
- java多线程之内存的可见性介绍(备用1)
(仅供参考) a.共享变量的可见能够一定程度保证线程安全,共享变量不可见导致数据不够准确,出现各种各样的问题,导致线程不安全. b.不同线程之间无法直接访问其他线程工作内存中的变量. 1.可见性 2. ...
- QuickClip—界面原型设计
1.需不需要设置用户登录/注册页? QuickClip没有提供该项功能.因为本产品为单纯的移动端视频编辑软件,是一个工具类软件.而且移动端软件本就追求的是方便快捷.简单易用,本产品不需要标识使用者的身 ...
- Linux常用命令——链接命令
链接命令:ln ln -s [原文件] [目标文件] 命令英文原意:link 功能描述:生成链接文件 选项:-s 创建软链接,也叫符号链接 硬链接特征: 1.拥有相同的i节点和存储block块,可以看 ...
- 2019西安多校联训 Day4
T1 大水题!!难度简单,显然的贪心策略即可,but... 思路:首先我们按与i点作战后活下来的士兵排序,然后 若当前剩余兵力足够直接减掉战斗死亡人数,如果不够就加 够再打它,但是!我们在考完试观察测 ...