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 ...
随机推荐
- H5页面背景音乐,C33 360°旋转效果
在做H5页面的时候,经常会需要用到背景音乐,比如电子贺卡.动态音乐相册等,右上角有个360°旋转的音乐图标,点击可以控制音乐是否播放,那这个效果是如何实现的呢?我现整理了一下代码: Demo 点击 ...
- 使用nginx加zuul配置
配置文件 $ ls -lrt -rw-r--r-- 1 root root 826 May 10 10:56 nginx.conf $ pwd /etc/nginx 增加配置 在http {}里 up ...
- swoole多进程处理产生的问题
以前用swoole的时候,没有涉及到数据库连接,碰到问题没有那么多,后来公司业务原生来写swoole多进程,问题出现很多 1.多进程之间会产生进程隔离,global无效,不能共用一个mysql,red ...
- laravel ORM 只开启created_at的方法
class User extends Model { //重写setUpdatedAt方法 public function setUpdatedAt($value) { // Do nothing. ...
- MvcPager 分页控件
官方教程: http://www.webdiyer.com/mvcpager
- 二分搜索poj106
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49944 Accepted: 10493 De ...
- daily_journal_2 神奇的一天
写博客日记的第二天,第一天立的flag开始有点松动啦,继续坚持啊!坚持就是胜利. 今天真是神奇的一天,上午的计划是照常进行的,但是前天淋雨赶上风寒,又吃了新疆室友的大补特产,龙体开始感觉到不适,于是上 ...
- dos 下小tip
tip 1:日期的格式化 方法如下: Echo %Date:~0,4%%Date:~5,2%%Date:~8,2%或者Set dt=%Date:~0,4%%Date:~5,2%%Date:~8,2%E ...
- python2行代码调用程序
import win32api win32api.ShellExecute(0, 'open', r'C:\Users\TOPFEEL\AppData\Local\Postman\app-5.5.0\ ...
- cocos creator 场景如何透明,多个canvas层级显示
转载地址:https://forum.cocos.com/t/creator-canvas/55373/14 Creator 版本:1.7 目标平台:WEB MOBILE 项目需要,页面做了多个Can ...