PAT 1107 Social Clusters
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<iostream> //集合的查并补
#include<vector>
#include<algorithm>
using namespace std;
vector<int> father, isroot;
bool cmp(const int& a, const int& b){
return a>b;
}
int findfather(int a){
int b=a;
while(father[a]!=a){
a=father[a];
}
while(b != father[b]) {
int z = b;
b = father[b];
father[z] = a;
}
return a;
}
void Union(int a, int b){ //并集
int faA= findfather(a);
int faB= findfather(b);
if(faA!=faB) father[faA]=faB;
}
int main(){
int N, cnt=0;
cin>>N;
vector<int> course(1001, 0);
father.resize(N+1);
isroot.resize(N+1);
for(int i=1; i<=N; i++)
father[i]=i;
for(int i=1; i<=N; i++){
int k;
scanf("%d:",&k);
for(int j=0; j<k; j++){
int t;
cin>>t;
if(course[t]==0)
course[t]=i;
Union(i, findfather(course[t]));
}
}
for(int i=1; i<=N; i++){
isroot[findfather(i)]++;
}
for(int i=1; i<=N; i++){
if(isroot[i]!=0)
cnt++;
}
sort(isroot.begin(), isroot.end(), cmp);
cout<<cnt<<endl;
for(int i=0; i<cnt; i++)
i==0?cout<<isroot[i]:cout<<" "<<isroot[i];
return 0;
}
PAT 1107 Social Clusters的更多相关文章
- 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——PAT甲级真题
1107 Social Clusters When register on a social network, you are always asked to specify your hobbies ...
- [并查集] 1107. Social Clusters (30)
1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...
- 1107 Social Clusters[并查集][难]
1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...
- pat甲级 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- PAT (Advanced Level) 1107. Social Clusters (30)
简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...
- 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)
题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...
随机推荐
- AngularJS2.0 quick start——其和typescript结合需要额外依赖
AngularJS2 发布于2016年9月份,它是基于ES6来开发的. 运行条件! 由于目前各种环境(浏览器或 Node)暂不支持ES6的代码,所以需要一些shim和polyfill(IE需要)让ES ...
- 洛谷 P4149 [ IOI 2011 ] Race —— 点分治
题目:https://www.luogu.org/problemnew/show/P4149 仍然是点分治: 不过因为是取 min ,所以不能用容斥,那么子树之间就必须分开算,记录桶时注意这个: 每次 ...
- bzoj3550
费用流+线性规划 搞了很长时间... 我们可以设立式子,a[1]+a[2]+a[3]+...+a[n]<=k , ... , a[2 * n + 1]+ ... +a[3*n]<=k a是 ...
- [Swift通天遁地]八、媒体与动画-(11)实现音乐播放的动态视觉效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- HDU 1754 Java
退役后学java... 裸线段树 //By SiriusRen import java.util.*; import java.math.*; public class Main{ public st ...
- 使用Jquery.form.js ajax表单提交插件弹出下载提示框
现象: 使用jquery的from做ajax表单提交的时候,后台处理完毕返回json字符串,此时浏览器提示下载一个json文件而不是在success里面继续解析该json对象. 具体的原因: 浏览器兼 ...
- 为什么现在改用int.TryParse了
以前一直用 int.Parse(x)或者 Convert.ToInt64(x),后来项目中发现如果x变量的值为null是,就报错了,哪怕我这样写 int.Parse(x=x??"0" ...
- 如何下载JDK和JRE历史版本
首先进入网址http://www.oracle.com/technetwork/java/javase/downloads/index.html 然后页面滑到最下面,选择[Java Archive]后 ...
- 开始玩qt,使用代码修改设计模式生成的菜单
之前制作菜单时,不是纯代码便是用设计模式 直接图形化完成. 今天我就是想用代码修改已经存在的菜单项,如果是用代码生成的可以直接调用指针完成: 但通过设计模式完成的没有暴露指针给我,至少我没发现. 在几 ...
- javascript DOM编程艺术 第10章问题记录
为什么moveElement函数调用时,必须加字符串的拼接符 var repeat = "moveElement('"+elementID+"',"+final ...