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

题目大意:有n个人,每个人喜欢k个活动,如果两个人有任意一个活动相同,就称为他们处于同一个社交网络。求这n个人一共形成了多少个社交网络。

第一个喜欢这个活动的称为教主,之后喜欢这个活动的人和教主unite即可.

1.并查集写错了,返回一个等号写成了两个等号.

2.找每组属于谁的时候可能还没有压缩路径,所以应该统计find(i)而不是par[i],,找分为几组的时候才可以用par[i]==i

#include <iostream>
#include<bits/stdc++.h>
#define each(a,b,c) for(int a=b;a<=c;a++)
#define de(x) cout<<#x<<" "<<(x)<<endl
using namespace std; const int maxn=1000+5;
const int inf=0x3f3f3f3f; int par[maxn];
int Rank[maxn];
int jiaozhu[maxn];
multiset<int>MS;
void init(int n)
{
for(int i=1;i<=n;i++)///初始化小错误
{
par[i]=i;
Rank[i]=0;
}
}
int find(int x)
{
return par[x]==x?x:par[x]=find(par[x]);///并查集的父节点满足par[x]==x,如果不是的话也要把par[x]更新,返回的是find(par[x])同时进行了路径的压缩
}
priority_queue<int>Q;
void unite(int x,int y)
{
//de(x);
//de(y);
x=find(x);
y=find(y); if(x==y)return;
if(Rank[x]<Rank[y])
{
par[x]=y;///军衔小,认y当爹
}
else
{
par[y]=x;
if(Rank[x]==Rank[y])Rank[x]++;///本来是兄弟,x却当了爹
}
}
int root_cnt[maxn];
/**
8
3: 2 7 10
1: 4
2: 5 3
1: 4
1: 3
1: 4
4: 6 8 1 5
1: 4
*/
int ans[maxn];
int cur=0;
int cmp(int a,int b){return a>b;}
int main()
{
int n;
scanf("%d",&n);
init(n);
cur=0;
each(i,1,n)
{
int k;
scanf("%d: ",&k);
while(k--)
{
int temp;
scanf("%d",&temp);
if(jiaozhu[temp]==0)
{
jiaozhu[temp]=i;
}
else
{
unite(jiaozhu[temp],i); }
}
}
each(i,1,n)
{
root_cnt[find(i)]++;
}
for(int i=1;i<=n;i++)
{
if(root_cnt[i]!=0)
{
ans[cur++]=root_cnt[i];
}
}
sort(ans,ans+cur,cmp);
printf("%d\n",cur);
each(i,0,cur-1)
{
printf("%d",ans[i]);
printf(i==cur-1?"\n":" ");
}
}

PAT-1107 Social Clusters (30 分) 并查集模板的更多相关文章

  1. 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)

    题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...

  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 (30)-PAT甲级真题(并查集)

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

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

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

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

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

  6. PAT (Advanced Level) 1107. Social Clusters (30)

    简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  7. PAT 1107 Social Clusters

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

  8. 1053 Path of Equal Weight (30分)(并查集)

    Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig ...

  9. 1107. Social Clusters (30)

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

随机推荐

  1. Java RMI实践

    Java远程方法调用,即Java RMI(Java Remote Method Invocation).一种用于实现远程过程调用的应用程序编程接口.客户机上运行的程序可以调用服务器上的对象. 缺点:只 ...

  2. C++操作Mysql数据库/Linux下

    本文链接:https://blog.csdn.net/Tanswer_/article/details/72796570想用C++写项目,数据库是必须的,所以这两天学了一下C++操作Mysql数据库的 ...

  3. Object.keys()、Object.values()、Object.entries()的用法

    一.Object.keys(obj) 参数:要返回其枚举自身属性的对象 返回值:一个表示给定对象的所有可枚举属性的字符串数组 处理对象,返回可枚举的属性数组 let person = {name:&q ...

  4. CopyOnWrite 思想在 Kafka 源码中的运用

    CopyOnWrite 思想在 Kafka 源码中的运用 在 Kafka 的内核源码中,有这么一个场景,客户端在向 Kafka 写数据的时候,会把消息先写入客户端本地的内存缓冲,然后在内存缓冲里形成一 ...

  5. Ionic4.x、Cordova Android 检测应用版本号、服务器下载文件以及实现App自动升级、安装

    Android App 升级执行流程 1.获取本地版本号 2.请求服务器获取服务器版本号 3.本地版本和服务器版本不一致提示升级,弹窗提示用户是否更新 4.用户确定升级,调用文件传输方法下载 apk ...

  6. 品优购商城项目(一)mybatis逆向工程

    第一阶段 dubboX和mybatis逆向工程 用了四天时间才完成品优购项目前两天的任务. 1.其中主要遇到的坑就是zookeeper服务消费者无法调用的问题.造成这个问题的主要原因就是忽略了dubb ...

  7. django 使用PyMySQL连接mysql

    * 安装pymysql模块 pip install pymysql * settings.py添加下面设置 ## pymysql repalce mysqldb import pymysql pymy ...

  8. source insight 使用配置(私人)

    1.输入两个空格,两个空格全消失,前后的字粘在一起显示,不想这样,就取消下图的勾.

  9. 123457123457#0#-----com.tym.PuzzleGame28--前拼后广--日常pt-tym

    com.tym.PuzzleGame28--前拼后广--日常pt-tym

  10. FormsAuthentication使用指南

    配置安全鉴别 鉴别是指鉴定来访用户是否合法的过程.ASP.NET Framework支持三种鉴别类型: Windows鉴别: NET Passport鉴别: Forms鉴别. 对于某一特定的应用程序, ...