PAT1107:Social Clusters
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
思路
并查集问题,course数组保存最先选择该课程的,source保存每个人关联的源点,然后查找合并就行。
代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;
vector<int> source(1001,0);
vector<int> course(1001,0); int findSource(int a)
{
int t = a;
while(source[a] != a)
{
a = source[a];
}
while(source[t] != t)
{
int tmp = t;
t = source[t];
source[tmp] = a;
}
return a;
} void Union(int a,int b)
{
a = findSource(a);
b = findSource(b);
if( a != b)
source[a] = b;
} bool cmp(int a,int b)
{
return a > b;
} int main()
{
int N;
while(cin >> N)
{
vector<int> root(N + 1,0);
for(int i = 1;i <= N;i++)
{
source[i] = i;
}
for(int i = 1;i <= N;i++)
{
int k;
scanf("%d : ",&k);
for(int j = 1;j <= k;j++)
{
int h;
cin >> h;
if(course[h] == 0)
course[h] = i;
Union(i,findSource(course[h]));
}
}
for(int i = 1;i <= N;i++)
{
++root[findSource(i)];
}
int cnt = 0;
for(int i = 1;i <= N;i++)
{
if(root[i] > 0)
cnt++;
}
cout << cnt << endl;
int flag = 0;
sort(root.begin(),root.end(),cmp); for(int i = 0;i < cnt;i++)
{
if(flag++ != 0)
cout << " ";
cout << root[i];
} }
}
PAT1107:Social Clusters的更多相关文章
- PAT-1107 Social Clusters (30 分) 并查集模板
1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...
- PAT甲级1107. Social Clusters
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...
- [并查集] 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 (并查集)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30 ...
- PAT_A1107#Social Clusters
Source: PAT A1107 Social Clusters (30 分) Description: When register on a social network, you are alw ...
- 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)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- A1107. Social Clusters
When register on a social network, you are always asked to specify your hobbies in order to find som ...
随机推荐
- 在android C/C++ native编程(ndk)中使用logcat
最近在研究Android 2.2 源代码的C/C++层,需要对代码进行一些调试,但是奇怪的是,直接添加LOGD("XXXXXXXX");,使用logcat却看不到任何输出,换成LO ...
- OpenCV 实现颜色直方图
颜色直方图是在许多图像检索系统中被广泛采用的颜色特征.它所描述的是不同色彩在整幅图像中所占的比例,而并不关心每种色彩所处的空间位置,即无法描述图像中的对象或物体.颜色直方图特别适于描述那些难以进行自动 ...
- STL常用查找算法介绍
adjacent_find() 在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器.否则返回past-the-end. #include <io ...
- 用LED灯和按键来模拟工业自动化设备的运动控制
开场白: 前面三节讲了独立按键控制跑马灯的各种状态,这一节我们要做一个机械手控制程序,这个机械手可以左右移动,最左边有一个开关感应器,最右边也有一个开关感应器.它也可以上下移动,最下面有一个开关感应器 ...
- 二分算法C实现
#include <stdio.h> #include <stdlib.h> #define NR(x) (sizeof(x)/sizeof(x[0])) int Binary ...
- SharePoint 添加BCD菜单
前言:在SharePoint中,我们常见的操作就是添加我们的自定义BCD菜单,下面,简单介绍下添加自定义BCD菜单的操作.主要介绍两种熟悉的方法,一种通过xml方式,另一种是通过js的方式. 环境:S ...
- Mac OS 终端常用命令基础
基础概念 OS X 采用的Unix文件系统,所有文件都挂在跟目录" /" 下面,所以不在要有Windows 下的盘符概念.比如什么"C:"你在桌面上看到的硬盘都 ...
- The 1st tip of DB Query Analyzer
The 1st tip of DB Query Analyzer Ma Genfeng (Guangdong Unitoll Services incorporate ...
- javascript原始值和对象引用
一句话来说:原始值是不可变的,而对象引用是可变的. js中的原始值(undefined.null.布尔值.数字和字符串)与对象(包括数组和函数)有着本质的区别.原始值是不可更改的,任何方法都无法更改一 ...
- redis存入中文,取出来显示不正常
问题: 127.0.0.1:6379> set name 张泰松OK127.0.0.1:6379> get name"\xe5\xbc\xa0\xe6\xb3\xb0\xe6\x ...