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 ...
随机推荐
- HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) ...
- ORA-01950:对表空间 'USERS' 无权限
在存储数据时出现ORA-01950:对表空间 'USERS' 无权限 错误,解决如下: 找到对象Users下的用户名,然后点编辑,角色权限添加dba 在oracle创建了一个新的表空间和一个新的用户, ...
- Oracle oerr工具介绍
(1)什么是oerr oerr是Oracle提供的在UNIX/Linux上查看Oracle错误的小工具,使用起来非常方便. (2)如何使用 oerr工具位于ORACLE_HOME下面,可以使用whic ...
- Webpack4 学习笔记七 跨域服务代理
webpack 小插件使用 webpack 监听文件变化配置 webpack 处理跨域问题 Webpack 小插件使用 clean-webpack-plugin: 用于在生成之前删除生成文件夹的Web ...
- Mongodb在window上启动
MongoDB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐.这里主要讲如何在windows平台下安装MongoDB. 安装最新版本mon ...
- JS模块化知识总结
背景 <script src="a.js"></script> <script src="b.js"></script ...
- multimap的使用
multimap由于允许有重复的元素,所以元素插入.删除.查找都与map不同. 插入insert(pair<a,b>(value1,value2)) #include <iostre ...
- 【TOJ 1743】集合运算(set并、交、差集)
Description 给定两个集合A和B的所有元素,计算它们的交.并.差集. Input 输入数据有多组,第一行为数据的组数T,接下来有2T行,每组数据占2行,每行有若干个整数,第一行的所有整数构成 ...
- JDBC编程:获取数据库连接
JDBC(Java Database Connectivity),即Java数据库连接.通过JDBC编程,可以使Java应用程序和数据库进行交互. JDBC驱动的方式有很多种,我们常用的驱动方式为:本 ...
- Solr6.6.0添加IK中文分词器
IK分词器就是一款中国人开发的,扩展性很好的中文分词器,它支持扩展词库,可以自己定制分词项,这对中文分词无疑是友好的. jar包下载链接:http://pan.baidu.com/s/1o85I15o ...