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 ...
随机推荐
- hive parition的使用,分dynamic和static两种
partition是hive提供的一种机制:用户通过指定一个或多个partition key,决定数据存放方式,进而优化数据的查询 一个表可以指定多个partition key,每个partition ...
- 一个 developer 的进化
作为一名开发者已十年,回顾过往大概经历了这么几个阶段,如下图所示: Develop Code 作为刚走出学校的学生进入公司,在最初的 1-2 年内就处于该阶段. 不停的开发代码,为系统的大厦添砖加瓦, ...
- 【65】Mybatis详解
Mybatis介绍 MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis能够 ...
- AngularJS进阶(十五)Cookie 'data' possibly not set or overflowed because it was too large
Cookie 'data' possibly not set or overflowed because it was too large (5287 > 4096 bytes)! 注:请点击此 ...
- java工具类(六)根据经纬度计算距离
Java实现根据经纬度计算距离 在项目开发过程中,需要根据两地经纬度坐标计算两地间距离,所用的工具类如下: Demo1: public static double getDistatce(double ...
- C语言之选择排序
选择法排序是相对好理解的排序算法.假设要对含有n个数的序列进行升序排列,算法步骤是: 1.从数组存放的n个数中找出最小数的下标(算法见下面的"求最值"),然后将最小数与第1个数交换 ...
- 手把手教你从头开始搭建友善之臂ARM-tiny4412开发环境(史上最详细!!)
创建一个ARM目录 mkdir /disk/A9 -p 接下来你需要准备以下的东西 1.arm-linux-gcc-4.5.1 交叉编译器 2.linux-3.5-tiny4412 ...
- ruby调用Office Jet引擎压缩access数据库
由于单位业务需要,搭建一台服务器专门用来做数据存储.因为数据流很小,遂采用access库作为DB.开始还好,但是后来发现access数据库有一问题,就是表空间会随着使用越来越大,哪怕表里没有数据.因为 ...
- IoC和DI的基本概念的思维导图
最近在学习Spring开发,IoC这个概念让我有点儿迷糊,控制反转这四个字是在是无法做到望文生义,于是乎就找了一些材料来学习,研究了半天,绘制了下面这幅思维导图.仅供参考!
- 重定向和servlet生命周期
重定向(1)什么是重定向服务器通知浏览器向一个新的地址发送请求.注:可以发送一个302状态码和一个Location消息头.(该消息头包含了一个地址,称之为重定向地址),浏览器收到之后,会立即向重定向地 ...