PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合。问总共有多少个集合,以及每个集合有多少人,并按从大到小输出。
很明显,采用并查集。vis[k]标记爱好k第一次出现的人的编号,如果为0则表示未出现。
当前第i个人若也存在爱好k,则只要将i与vis[k]两个人合并即可。
最后father[i]相同的即处在同一个集合中。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
/*
并查集
*/
const int maxn=;
int vis[maxn]; //vis[i]标记某爱好第一次出现在第几个人,0表示还未出现。
int n; struct UF{
int father[maxn];
void init(){
for(int i=;i<maxn;i++){
father[i]=i;
}
}
int find_root(int x){
if(father[x]!=x){
father[x]=find_root(father[x]);
}
return father[x];
}
void Union(int x,int y){
int fx=find_root(x);
int fy=find_root(y);
if(fx!=fy){
father[fy]=fx;
}
}
}uf; bool cmp(int a,int b){
return a>b;
}
int main()
{
char str[];
int num,a;
scanf("%d",&n);
memset(vis,-,sizeof(vis));
uf.init();
for(int i=;i<=n;i++){
scanf("%s",str);
int len=strlen(str);
str[len-]='\0';
num=atoi(str);
for(int k=;k<num;k++){
scanf("%d",&a);
if(vis[a]==-)
vis[a]=i;
else{
uf.Union(vis[a],i); //若已出现过,则第i个人与第vis[a]个人合并,即分为一组
}
}
}
int cnt[n+];
int res=;
memset(cnt,,sizeof(cnt));
for(int i=;i<=n;i++){
int idx=uf.find_root(i);
if(cnt[idx]==)
res++;
cnt[idx]++;
}
sort(cnt+,cnt+n+,cmp);
printf("%d\n",res);
for(int i=;i<res;i++)
printf("%d ",cnt[i]);
printf("%d",cnt[res]);
return ;
}
PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)的更多相关文章
- 【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)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- 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 (Advanced Level) 1107. Social Clusters (30)
简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)
题意:给了图,以及s和t,让你求s到t花费的最短路程.最短时间,以及输出对应的路径. 对于最短路程,如果路程一样,输出时间最少的. 对于最短时间,如果时间一样,输出节点数最少的. 如果最短路程 ...
- 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 ...
- PAT-1107 Social Clusters (30 分) 并查集模板
1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...
随机推荐
- October 16th 2017 Week 42nd Monday
The more decisions that you are forced to make alone, the more you are aware of your freedom to choo ...
- python提示警告InsecureRequestWarning
在Python3中使用以下代码报错: import requests response = requests.get(url='', verify=False) 错误代码如下: InsecureReq ...
- Spring 事务回滚代码
在事务中实行的方法:org.springframework.transaction.interceptor.TransactionAspectSupport#invokeWithinTransacti ...
- 网站横幅切换jquery 插件
最近做一个web项目,站点首页需要像 百度统计 页面类似的切换横幅,有兴趣的可以先看看它的效果,这样就比较容易理解为什么我单独做个插件.其实类似的 jquery 插件网上类似的多的去了,随便网上下了两 ...
- spring引入properties变量报错
通过properties配置文件配置数据源,代码如下: <bean class="org.springframework.beans.factory.config.PropertyPl ...
- nginx下No input file specified错误的解决
在web服务的根目录下创建 .htaccess文件,设置一下内容: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond % ...
- Python2.7-difflib
difflib主要用于比较两个序列的不同,常见于字符串的比较,可以对差异生成报告.SequenceMatcher 主要用于找两者相似部分,以及两者不同的转换方法,而 Differ 更注重于比较两者的区 ...
- Kafka设计解析(二十二)Flink + Kafka 0.11端到端精确一次处理语义的实现
转载自 huxihx,原文链接 [译]Flink + Kafka 0.11端到端精确一次处理语义的实现 本文是翻译作品,作者是Piotr Nowojski和Michael Winters.前者是该方案 ...
- Foxmail添加gmail密码错误
想在foxmail上添加gmail时一直报密码错误,找了一圈发现是开启了两步验证,需要用应用专用密码才可以登录,生成应用专用密码的地址如下: https://security.google.com/s ...
- Python中 __init__的通俗解释?附修饰器contextmanager的理解
作者:匿名用户链接:https://www.zhihu.com/question/46973549/answer/103805810来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...