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 ...
随机推荐
- CUDA学习,查看device性能参数
#include "../common/book.h" #include< stdio.h> #include "cuda_runtime.h" # ...
- 11_Eclipse中演示Git版本的创建,历史版本的修改,创建分支,合并历史版本和当前版本
1 执行以下案例: 某研发团队2011年初开发了一款名为Apollo的信息系统,目前已发布v1.0版本.此项目初期已有部分基础代码, 研发团队再此基础代码上经过3个月的努力发布了一个功能相对完备 ...
- 【一天一道LeetCode】#19. Remove Nth Node From End of List
一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...
- 不错的东西: AutoMapper
详细信息可阅读原文:http://csharppulse.blogspot.in/2013/08/crud-operations-using-automapper-in-c_381.html 这东西可 ...
- How to Simulate the Price Order or Price Line Function using API QP_PREQ_PUB.PRICE_REQUEST Includes
How to Simulate the Price Order or Price Line Function using API QP_PREQ_PUB.PRICE_REQUEST Includes ...
- AngularJS进阶(一)深入理解ANGULARUI路由_UI-ROUTER
深入理解ANGULARUI路由_UI-ROUTER 最近在用 ionic写个webapp 看到几个demo中路由有好几种,搞的有点晕,查下资料研究下,做个笔记,其中大部分为摘抄别人的,做个说明免得被人 ...
- saiku应用的调试
ubuntu下解压saiku包后使用: 运行.sh命令(.bat是windows命令).运行时注意权限.可以先chmod a+x *.sh 提示,catali?.sh出错. 这是tomcat的一个文件 ...
- ActiveMQ系列之四:用ActiveMQ构建应用
Broker:相当于一个ActiveMQ服务器实例 命令行启动参数示例如下: 1:activemq start :使用默认的activemq.xml来启动 2:activemq start xbean ...
- 史上最简单的C语言链表实现,没有之一
#include <stdio.h> #include <string.h> #include <stdlib.h> #define NR(x) (sizeof(x ...
- Java中使用C3P0连接池
先看官网给的范例: import java.sql.*; import javax.naming.*; import javax.sql.DataSource; import com.mchange. ...