【PAT甲级】1107 Social Clusters (30分)(非递归并查集)
题意:
输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号。拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数(不同俱乐部的两个人的爱好一定不相同)。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
vector<int>v[],st;
int fa[];
int a[];
int find_(int x){
int k,j,r;
r=x;
while(r!=fa[r])
r=fa[r];
k=x;
while(k!=r){
j=fa[k];
fa[k]=r;
k=j;
}
return r;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
st.resize(n+);
for(int i=;i<=n;++i)
fa[i]=i;
for(int i=;i<=n;++i){
int x;
cin>>x;
cin.ignore();
for(int j=;j<=x;++j){
int y;
cin>>y;
v[i].push_back(y);
}
}
for(int i=;i<=n;++i)
for(auto it:v[i])
if(!a[it])
a[it]=i;
else{
int x=find_(i);
int y=find_(a[it]);
if(x!=y)
fa[x]=y;
}
for(int i=;i<=n;++i)
++st[find_(i)];
int cnt=;
for(int i=;i<=n;++i)
if(st[i])
++cnt;
cout<<cnt<<"\n";
sort(st.rbegin(),st.rend());
for(int i=;i<cnt;++i){
cout<<st[i];
if(i<cnt-)
cout<<" ";
}
return ;
}
【PAT甲级】1107 Social Clusters (30分)(非递归并查集)的更多相关文章
- 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甲级1107. Social Clusters
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...
- PAT甲级——1107 Social Clusters (并查集)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 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 ...
- [并查集] 1107. Social Clusters (30)
1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...
- 非递归并查集——zoj4109
卡常卡的我难受 非递归并查集好像写起来常数小一点 int F[maxn]; int Find(int x){ int r = x; while (F[r] != r)r = F[r]; int i = ...
- 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 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)
1080 Graduate Admission (30 分) It is said that in 2011, there are about 100 graduate schools ready ...
- PAT 甲级 1072 Gas Station (30 分)(dijstra)
1072 Gas Station (30 分) A gas station has to be built at such a location that the minimum distance ...
随机推荐
- PHP函数对比 array_merge()与加号合并数组的区别
首先准备两个数组,从数组索引类型分别讨论. 数组索引为字符串索引时: $a = array('a' => 1, 'b' => 2, 'c' => 3); $b = array('b' ...
- Java 13 新特性
一.core-libs/java.nio 添加 FileSystems.newFileSystem(Path, Map<String, ?>) 方法 java.nio.file.FileS ...
- 2.4测试赛AC代码临时保存
//H #include<cstdio> #include<cstdlib> #include<cstring> #include<stack> usi ...
- ASP.NET Process Model之二:ASP.NET Http Runtime Pipeline - Part II
https://www.cnblogs.com/artech/archive/2007/09/13/891266.html 二.ASP.NET Runtime Pipeline(续ASP.NET Ht ...
- 【音乐欣赏】《Heart Made of Stone》 - The Tech Thieves
曲名:Heart Made of Stone 作者:The Tech Thieves Yeah It's been years now and I wonder Is it over? Do you ...
- 后台异常 - Unrecognized Windows Sockets error: 0: JVM_Bind
问题描述 LifecycleException: Protocol handler initialization failed: java.net.SocketException: Unrecogn ...
- Unity3d简便的声音管理方案
本方法是对Ez-Sound-Manager的扩展 https://github.com/JackM36/Eazy-Sound-Manager 参考Audio Toolkit Free Version ...
- mysql 原有的主键情况下设置自增字段
mysql 的自增字段只能是主键,如果原表已经有主键,需要设置自增字段应该怎么做呢? 1.alter table bu_staff drop primary key; 先删除表的主键 id为原表 ...
- window环境下获取python安装的路径
1.cmd + win 打开命令行 2.where python
- EF database first
https://www.cnblogs.com/net064/p/8024150.html 1.EF简介ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对 ...