PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
题目链接:点击打开链接
题目大意:一个“社交集群”是指部分兴趣爱好相同的人的集合。你需要找出所有的社交集群。
解题思路:并查集思路,轮到第几个人时,以它的 id 为 root,然后吸收它自己所有的兴趣爱好编号,这样就可以达到有部分兴趣爱好相同的人在一个集群里的效果。
AC 代码
#include<bits/stdc++.h>
#include<cmath>#define mem(a,b) memset(a,b,sizeof a)
#define ssclr(ss) ss.clear(), ss.str("")
#define INF 0x3f3f3f3f
#define MOD 1000000007using namespace std;typedef long long ll;int pre[2009], vis[1009], cnt[2009];void init()
{for(int i=1;i<=2009;i++) pre[i]=i;
}int findx(int x)
{return pre[x]==x ? x : pre[x]=findx(pre[x]);
}void join(int x,int y)
{int fx=findx(x), fy=findx(y);if(fx!=fy) pre[fx]=fy;
}int main()
{init();int n,k,t,id;scanf("%d",&n);for(int i=1;i<=n;i++){id=1000+i;scanf("%d:",&k);while(k--){scanf("%d",&t);join(t,id);}vis[i]=t; // 记录其中一个点即可,因为都是连结在一起的}for(int i=1;i<=n;i++){id=findx(vis[i]);cnt[id]++;}sort(cnt,cnt+2009,greater<int>());int l;for(l=0;;l++) if(cnt[l]==0) break;printf("%d\n",l);for(int i=0;i<l;i++) printf("%d%c",cnt[i],i==l-1?'\n':' ');return 0;
}
PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)相关推荐
- PAT甲级1107 Social Clusters (30 分):[C++题解]并查集,爱好、人数
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 凭爱好,分人群.注意点:爱好可传递.什么意思?意思是A和B的有共同爱好1, B和C有共同爱好2,那么认为A和C也是同一群人. 按照爱 ...
- 【PAT (Advanced Level) Practice】1008 Elevator (20 分)
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #incl ...
- 1107 Social Clusters (30 分)【难度: 中 / 知识点: 并查集】
https://pintia.cn/problem-sets/994805342720868352/problems/994805361586847744 很不错的一个并查集的操作.你会发现我们需要处 ...
- PAT (Advanced Level) Practice 题解代码 - II (1051-1100)
PAT PAT (Advanced Level) Practice - II(1051-1100) -------------------------------------------------- ...
- PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642
PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642 题目描述: A Binary Search Tr ...
- PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642
PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...
- PAT (Advanced Level) Practice 1053 Path of Equal Weight (30 分)
1053 Path of Equal Weight (30 分) Given a non-empty tree with root R, and with weight Wi assigned to ...
- PAT (Advanced Level) Practice 题目集合(1001 ~ 1050)(正在更新)
1001 A+B Format (20 分) 题目大意:计算a+b,结果按照西方的那种写数字的方式输出,从三个数一个逗号那种. #include<bits/stdc++.h> using ...
- 【PAT (Advanced Level) Practice】1099 Build A Binary Search Tree (30 分)
深搜+广搜 #include <iostream> #include <stack> #include <queue> #include <vector> ...
最新文章
- Linux动态库和静态库比较
- 分布式事务中间件 Fescar—RM 模块源码解读
- rpm安装mysql图文
- java内置_自包含的应用程序,内置Java
- 计算机网络复习-应用层
- opengl加载显示3D模型HMP类型文件
- Spring Cloud比较好的博文地址
- SQL Server T-SQL编程:数据库用户与安全设置
- menu---A.2-基本算法实例
- ntext字段的REPLACE处理示例.sql
- Linux命令收藏-进程管理
- 高岭土吸附阳离子_水分子在高岭土中吸附特性的蒙特卡罗模拟研究
- Kotlin入门(31)JSON字符串的解析
- linux基础学习之关机命令总结
- 设计模式 ( 十三 ) JDK动态代理模式
- ESC/POS协议打印机工具类(java)
- 语音信号处理及特征提取
- 软件测试工程师必备的27个基础技能
- 基于Arduino的智能小车-代码部分
- hdu 4399 树形dp
热门文章
- ASP连接ACCESS2007数据库时注意事项
- CoolShell-第1题
- 计算机语言及其用法,计算机语言不同用法的五大分类及优缺点
- android常用库
- 水货笨叔介绍MCS锁
- 2021不多BB不走弯路,我认为我这份Java学习路线是适合绝大多数人的
- 2022 年江西省职业院校技能大赛高职组“信息安全管理与评估”赛项样题
- 微软Build 2017开发者大会午夜趴
- Domain Adaptive Object Detection for Autonomous Driving under FoggyWeather(翻)
- 文章入选人工智能内容榜第一名