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 P1801 黑匣子_NOI导刊2010提高(06)】 题解
题目链接:https://www.luogu.org/problemnew/show/P1801 替罪羊树吼啊! #include <cstdio> #include <cstrin ...
- Java8函数之旅 (七) - 函数式备忘录模式优化递归
前言 在上一篇开始Java8之旅(六) -- 使用lambda实现Java的尾递归中,我们利用了函数的懒加载机制实现了栈帧的复用,成功的实现了Java版本的尾递归,然而尾递归的使用有一个重要的条件就是 ...
- Javascript文件中的控制器I
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Java 加密Excel文件(打开时需输入密码)
收集:author: lifq package com.XXX.XXX.utils; import java.io.IOException; import com.jxcell.CellExcepti ...
- webpack——安装报错及解决办法
①先删除 删除全局webpack-cli webpack4.x开始官方文档是说要安装cli所以如果的在用4.+就需要卸载cli npm uninstall -g webpack-cli # 注释给我这 ...
- js函数只触发一次
如何让js中的函数只被执行一次?我们有时候会有这种需求,即让一个函数只执行一次,第二次调用不会返回任何有价值的值,也不会报错.下面将通过三个小demo展示使用的方法,当做个人笔记. 1.通过闭包来实现 ...
- 实践笔记-VA05 销售订单清单 增加字段
现在都自开发很多报表 ,估计没有多少人 用 VA05 1.在结构 VBMTVZ 中增加需要的字段 2.表t180a 中 添加一条 “添加字段”的数据,如下: 3.取值 修改程序 INCLUDE V ...
- MySQL——用户与密码
mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码.通过下面的方式找到root默认密码,然后登录mysql进行修改: grep 'temporary p ...
- Linux系统文件和目录的属性及权限
1 文件属性概述 Linux系统中的文件或目录的属性主要包括:索引节点(inode).文件类型.权限属性.硬链接数.所归属的用户和用户组.最近修改时间等内容(文件名严格来说不属于文件的属性): 下面是 ...
- ASP.NET Web用户控件
用户控件可用来实现页面中可重用的代码,是可以一次编写就多处方便使用的功能块.它们是 ASP.NET控件封装最简单的形式.由于它们最简单,因此创建和使用它们也是最简单的.用户控件实际上是把已有的服务器控 ...