1107 Social Clusters (30 分)(并查集)
并查集的基本应用
#include<bits/stdc++.h> using namespace std;
const int N=1e3+;
vector<int>vec[N];
int p[N];
const int inf=0x3f3f3f3f;
int isroot[N]={};
int course[N]={};
int findth(int x)
{
if(x==p[x]) return x;
return p[x]=findth(p[x]);
} void unionn(int x,int y)
{
int xx=findth(x);
int yy=findth(y);
if(xx!=yy){
p[yy]=xx;
}
} bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=N;i++) p[i]=i;
for(int i=;i<=n;i++){
int k;
char ch;
scanf("%d%c",&k,&ch);
for(int j=;j<k;j++){
int x;
scanf("%d",&x);
if(course[x]==){
course[x]=i;//这个相当于标记一下,如果x这个活动没有人选就把i赋值给他 如果有了 就把他和i放在一个并查集里
}
unionn(i,course[x]);
}
}
for(int i=;i<=n;i++){
isroot[findth(i)]++;//这个是求有几个集合 每个集合里有几个常用的算法
}
int ans=;
for(int i=;i<=n;i++){
if(isroot[i]!=) ans++;
}
sort(isroot+,isroot+n+,cmp);
printf("%d\n",ans);
for(int i=;i<=ans;i++){
if(i!=) printf(" ");
printf("%d",isroot[i]);
}
printf("\n"); return ;
}
1107 Social Clusters (30 分)(并查集)的更多相关文章
- 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 (30分)(非递归并查集)
题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...
- [并查集] 1107. Social Clusters (30)
1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...
- 1107 Social Clusters (30)(30 分)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- pat甲级 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- PAT (Advanced Level) 1107. Social Clusters (30)
简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- 1053 Path of Equal Weight (30分)(并查集)
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weig ...
- 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- PAT甲级——1107 Social Clusters (并查集)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30 ...
随机推荐
- 【luogu P1640 [SCOI2010]连续攻击游戏】 题解
题目链接:https://www.luogu.org/problemnew/show/P1640 数据有点水吧,从属性值连向对应武器编号. 枚举属性值匹配,遇到第一个无法匹配的直接跳出就好惹~. #i ...
- 【luogu P3379 最近公共祖先】 模板
题目链接:https://www.luogu.org/problemnew/show/P3379 倍增求lca,先存下板子,留个坑以后再填讲解. in 5 5 43 12 45 11 42 43 23 ...
- 纯JS拖动案例
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- PThread 学习笔记
POSIX 线程,也被称为Pthreads,是一个线程的POSIX标准: pthread.h int pthread_create(pthread_t * thread, pthread_attr_t ...
- 在Win7虚拟机下搭建Hadoop2.6.0伪分布式环境
近几年大数据越来越火热.由于工作需要以及个人兴趣,最近开始学习大数据相关技术.学习过程中的一些经验教训希望能通过博文沉淀下来,与网友分享讨论,作为个人备忘. 第一篇,在win7虚拟机下搭建hadoop ...
- samba文件共享服务的配置
samba文件共享服务的配置 服务端配置 一.安装samba软件包 命令:yum -y install samba 查看是否安装samba. [root@Centos7-Server haha]# [ ...
- 【shell脚本学习-2】
#!/bin/bash - #echo do you have exetuate this project \n printf "please input your passwd" ...
- 揭开js之constructor属性的神秘面纱
揭开 constructor 在 Javascript 语言中,constructor 属性是专门为 function 而设计的,它存在于每一个 function 的prototype 属性中.这个 ...
- wamp环境下安装imagick扩展
先上图,如下是安装成功后的phpinfo()界面: 安装步骤: 1.先确定安装版本,比如我的的php : php7.0.12 x86 ts 那么就需要三方版本 要一致:imagick软件本身( 如x ...
- 微信小程序横向滚动
<scroll-view scroll-x="true" style=" white-space: nowrap; display: flex" > ...