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 ...
随机推荐
- webstorm开发nodejs的技巧--代码提示DefinitelyTyped
先发视频“WebStorm - MEAN Stack Walkthrough and Tips”,地址:http://www.tudou.com/programs/view/txhBUA7fcNA/? ...
- Asset Store 下载的package存在什么地方?
发现从Asset store下载的packages都不知道放在了什么地方 Windows 7,C:\Users\<username>\AppData\Roaming\Unity\Asset ...
- JavaScript:改变 HTML 样式
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- JS中遍历数组、对象的方式
1.标准的for循环遍历数组 //不打印自定义属性和继承属性 var array = [1,2,3]; for (var i = 0; i < array.length; i++) { cons ...
- JavaScript 基础(六) 数组方法 闭包
在一个对象中绑定函数,称为这个对象的方法.在JavaScript 中,对象的定义是这样的: var guagua = { name:'瓜瓜', birth:1990 }; 但是,如果我们给瓜瓜绑定一个 ...
- python查找目录及子目录下特定文件
写这篇博客的缘由: 面试归来翻脉脉发现一个陌生的朋友提出一个面试题,设计实现遍历目录及子目录,抓取.pyc文件. 并贴出两种实现方法: 个人感觉,这两种方法中规中矩,不像是python的风格.pyth ...
- 小程序登录 -41003: aes 小程序加密数据解密失败问题
在微信小程的开发中,登录问题,一定要按照这样的顺序 1. 小程序请求login,拿到code 然后传给服务端: 2.服务端拿到code 到微信服务器拿到sessionKey :3.然后小程序调用ge ...
- mysql的数据操作和内置功能总结
一.数据的增删改查 1.插入数据 a.插入完整数据(顺序插入) INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n); INSERT INTO 表名 ...
- Python全栈day 02
Python全栈day 02 一.循环语句 while 用法 num = 1 while num <= 10: print(num) num += 1 # 循环打印输出1-10 while el ...
- Java学习笔记十四:如何定义Java中的类以及使用对象的属性
如何定义Java中的类以及使用对象的属性 一:类的重要性: 所有Java程序都以类class为组织单元: 二:什么是类: 类是模子,确定对象将会拥有的特征(属性)和行为(方法): 三:类的组成: 属性 ...