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:

K​i​​: h​i​​[1] h​i​​[2] ... h​i​​[K​i​​]

where K​i​​ (>0) is the number of hobbies, and h​i​​[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

题目大意:根据输入将用户分堆,也就是并查集的题目;输入格式:一共几个人,第几个人一共有几个爱好:爱好序号。根据兴趣爱好进行分堆,看哪些人在一起了,并按人数非升序排列。

//题目中给的样例共有3个簇,最大的4个人包括 2,4,6,8四个人,3个人的包括3,5,7三个人,这种没有重复,但是有没有可能会重复呢?

//我能想到的就是创建1000个向量,每个用户分别push,然后求出来长度!=1的向量,然后按照长度大小输出,其他没什么想法了。。。

代码来自:https://www.liuchuo.net/archives/2183

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> father, isRoot;
int cmp1(int a, int b){return a > b;}
int findFather(int x) {
int a = x;
while(x != father[x])//先找到整棵树的根节点。
x = father[x];
while(a != father[a]) {//找父亲的时候顺便将其高度变短,父节点都变为根节点了。
int z = a;//你学过的!变成两层高的。
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a, int b) {
int faA = findFather(a);
int faB = findFather(b);
if(faA != faB) father[faA] = faB;
}
int main() {
int n, k, t, cnt = ;
int course[] = {};
scanf("%d", &n);
father.resize(n + );
isRoot.resize(n + );
for(int i = ; i <= n; i++)
father[i] = i;
for(int i = ; i <= n; i++) {
scanf("%d:", &k);
for(int j = ; j < k; j++) {
scanf("%d", &t);
if(course[t] == )
course[t] = i;//也就是将这个课程归并为i所有。
Union(i, findFather(course[t]));//找到这个课程的父亲,因为两者有相同的父亲,
//此处应该findFather(course[t])是父亲的,因为其是course的编号。
//是一个簇内的,那么合并即可。
}
}
for(int i = ; i <= n; i++)
isRoot[findFather(i)]++;//那些是根节点的都++;最终求出来的是簇内所含的人数。
for(int i = ; i <= n; i++) {
if(isRoot[i] != ) cnt++;
}
printf("%d\n", cnt);
sort(isRoot.begin(), isRoot.end(), cmp1);
for(int i = ; i < cnt; i++) {
printf("%d", isRoot[i]);
if(i != cnt - ) printf(" ");
}
return ;
}

//真的太神奇了。

1.couser[i]表示爱好i第一次出现时的用户编号,如果=0表示这个爱好没出现。

2.如果新来了一个人k爱好也为i,那么将couser[i]和Kunion即可。

3.复习了并查集,并查集三要素:findFather+union+初始化father数组为自己;

4.这里使用了一个for循环遍历来确定每个簇有多少个人。

1107 Social Clusters[并查集][难]的更多相关文章

  1. PAT甲级——1107 Social Clusters (并查集)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30  ...

  2. [并查集] 1107. Social Clusters (30)

    1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...

  3. PAT甲级1107. Social Clusters

    PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...

  4. 1107 Social Clusters——PAT甲级真题

    1107 Social Clusters When register on a social network, you are always asked to specify your hobbies ...

  5. PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)

    题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...

  6. 1107 Social Clusters (30 分)(并查集)

    并查集的基本应用 #include<bits/stdc++.h> using namespace std; ; vector<int>vec[N]; int p[N]; con ...

  7. 1107 Social Clusters (30)(30 分)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  8. pat甲级 1107. Social Clusters (30)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  9. PAT 1107 Social Clusters

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

随机推荐

  1. excel中,一系列单元格中包含某一个字段的单元格数量?

    excel中,一系列单元格中包含某一个字段的单元格数量?这个怎么写公式?如:A列单元格A1-A7的内容分别为 A.AB.BC.AC.CD.AD.EA,怎么数这一列中几个单元格的内容包含A字母? 任意单 ...

  2. python2.0 s12 day8 _ 堡垒机前戏paramiko模块

    堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 paramiko模块是做主机管理的,他模拟了一个ssh. 有两种形式连接形式, ...

  3. ios学习--TableView详细解释

    -.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; [DataTa ...

  4. 程序启动-Runloop

    0 从程序启动开始到view显示: start->(加载framework,动态静态链接库,启动图片,Info.plist,pch等)->main函数->UIApplicationM ...

  5. MQTT 3.1协议非严肃反思录

    前言 MQTT 3.1协议在弱网络环境下(比如2G/3G等)表现不够好,因此才有了反思. 弱网环境下表现 手机等终端在弱网络环境下丢包情况会非常明显,连接MQTT Server成功率很低.相比单纯的请 ...

  6. MQTT的学习研究(十五) MQTT 和android整合文章

    详细参考:  How to Implement Push Notifications for Android http://tokudu.com/2010/how-to-implement-push- ...

  7. 银联支付-产品测试sdk使用流程

    准备工作: 到https://open.unionpay.com/ajweb/help/file/techFile?productId=66下载开发文档和sdk 下载之后进行解压将Java Versi ...

  8. Kotlin——初级篇(一):最详细的环境搭建

    众所周知,Kotlin出来已经良久了.Kotlin有着众多优势,不管是用于Android开发中,还是Java开发,都能缩减很大的代码量,大大提高了工作效率.而小生本人也是才从忙碌的个工作中抽身出来,有 ...

  9. Linux学习(四)档案与目录管理

    1. 目录与路径  1.1 相对路径与绝对路径  1.2 目录的相关操作: cd, pwd, mkdir, rmdir  1.3 关于执行文件路径的变量: $PATH2. 档案与目录管理  2.1 档 ...

  10. [黑金原创教程] FPGA那些事儿《数学篇》- CORDIC 算法

    简介 一本为完善<设计篇>的书,教你CORDIC算法以及定点数等,内容请看目录. 贴士 这本教程难度略高,请先用<时序篇>垫底. 目录 Experiment 01:认识CORD ...