A1107. 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<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int cnt[] = {,}, father[], hobby[] = {,};
int N;
int findFather(int x){
int a = x;
while(father[x] != x){
x = father[x];
}
while(a != x){
int temp = a[father];
a[father] = x;
a = temp;
}
return x;
}
void union_(int a, int b){ //a集合的根节点不变
int tempa = findFather(a);
int tempb = findFather(b);
if(tempa != tempb){
father[tempb] = a;
}
}
bool cmp(int a, int b){
return a > b;
}
int main(){
int N;
scanf("%d", &N);
for(int i = ; i <= N; i++){
father[i] = i;
}
for(int i = ; i <= N; i++){
int num, temp;
scanf("%d:", &num);
for(int j = ; j < num; j++){
scanf("%d", &temp);
if(hobby[temp] == ){
hobby[temp] = i;
}else{
union_(hobby[temp], i);
}
}
}
for(int i = ; i <= N; i++){
int root = findFather(i);
cnt[root]++;
}
int ans = ;
for(int i = ; i <= N; i++){
if(cnt[i] != )
ans++;
}
sort(cnt, cnt + N + , cmp);
printf("%d\n%d", ans, cnt[]);
int k = ;
while(k < N && cnt[k] != ){
printf(" %d", cnt[k]);
k++;
}
cin >> N;
return ;
}
总结:
1、题意:看了书上的解释才搞明白这道题什么意思。并不是说一个set里的所有人都要有共同的hobby,而是当两个人有至少一个共同hobby时,他们就处于一个set。比如A的hobby是1、2,B的hobby是2、3,则AB在一个set中。而C的hobby是3、4,则B、C在一个set中。AB同set,BC同set,则ABC同属一个set。其实就是考并查集。
2、并查集的要点:
- father[a] = b,表示a的父节点是b。若father[i] = i,则i是根节点。 初始化时,每个节点都初始化为根节点。
- 查找根节点:当x != father[x] 时,不断进行x = father[x] 的操作即可。
- 合并:为防止出现环路,只能对不同的集合做合并。所以合并a、b时,先找到a的根节点roota, b的根节点rootb,如果roota != rootb, 则 father[rootb] = roota。严格按这个流程做可以避免出错。不要仅仅把b的父亲设为roota。
- 路径压缩:为了降低查找的复杂度。可以放在查找根节点的函数中,当找到 x 的根节点 root 时,再从 x 往根节点回溯一次,沿途所有节点的父节点均设置为 root。
3、只有两个人有共同hobby才将他们所在的两个set做合并,但如果对每个人保存一个hobby列表,然后每两个人检查是否有共同爱好会很费时。可以设置一个hobby数组,hobby[i]仅仅记录一个人,就是首个读入的有这个爱好的人,并将这个人作为他所在set的root节点一直不变。
A1107. Social Clusters的更多相关文章
- PAT A1107 Social Clusters (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 ...
- PAT_A1107#Social Clusters
Source: PAT A1107 Social Clusters (30 分) Description: When register on a social network, you are alw ...
- PAT1107:Social Clusters
1107. Social Clusters (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue When ...
- [并查集] 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
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...
- PAT甲级——1107 Social Clusters (并查集)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30 ...
- PAT-1107 Social Clusters (30 分) 并查集模板
1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...
随机推荐
- js中的call、apply、bind
在js中每个函数都包含两个非继承而来的方法:call()和apply() call和apply的作用都是在特定的作用域中将函数绑定到另外一个对象上去运行,即可以用来重新定义函数的执行环境,两者仅在定义 ...
- Ubuntu18.04安装mysql5.7
Ubuntu18.04安装mysql5.7 1.1安装 首先执行下面三条命令: # 安装mysql服务 sudo apt-get install mysql-server # 安装客户端 sudo a ...
- Redis事物
redis事物定义: >Redis事务是一个单独的隔离操作:事务中的所有命令都会序列化.按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的命令请求所打断. >Redis事务的主要作 ...
- WPF如何实现TreeView节点重命名
我们经常看到一些软件比如酷狗音乐,在对列表右键进行重命名的时候,当前列表会泛白并且进入可编辑状态,当我们更改完成后就会并进入非编辑状态,这些具体是怎么实现的呢?下面的方法也许会提供一些思路,下面的Tr ...
- 记一次生产mysql数据误操作恢复过程
提示:建议每次对数据库进行修改时都做下备份 注意:以下Mysql开启的是row格式的binlog日志,确定到误操作具体时间可能有些麻烦,默认的格式就能很快找出来.这里开启row的原因是还有一种更快的方 ...
- Javassist之常用API的应用 02
测试模型代码: package org.study2.JavaSenior.annotation.javassistDemo; /** * @Auther:GongXingRui * @Date:20 ...
- CS新建排版
1.拉菜单栏barmanage,去掉不要的头部和尾部 ,选择控件bar属性optionsbar 全部为false,防止菜单拖动. 2.拉一个panelcontrol属性dock 设置顶部,在拉一个p ...
- javascript帧动画
前面的话 帧动画就是在“连续的关键帧”中分解动画动作,也就是在时间轴的每帧上逐帧绘制不同的内容,使其连续播放而成的动画.由于是一帧一帧的画,所以帧动画具有非常大的灵活性,几乎可以表现任何想表现的内容. ...
- Microsoft Azure Machine Learning Studio
随着机器学习(ML)成为软件行业的主流,重要的是要了解它的工作原理,并将其置于开发栈中.了解如何为您的应用程序构建ML服务,您可以确定您的ML应用程序中的机会,实施ML,并与您的团队的ML专业人士清楚 ...
- Mybatis常见问题总结
1.大于号.小于号在sql语句中的转换 使用mybatis 时sql语句是写在xml文件中,如果sql中有一些特殊的字符的话,比如< ,<=,>,>=等符号,会引起xml格式的 ...