The Suspects (并查集(裸))
来源:互联网 发布:淘宝脱胶险怎么设置 编辑:程序博客网 时间:2024/06/05 19:36
题目来源:https://vjudge.net/problem/POJ-1611
【题意】
给出n给点(0到n-1),m个阵营,其中0号是嫌疑犯,与他在同一阵营的都是嫌疑犯,并且一个人可能身在不同的阵营。问,嫌疑犯有多少人。
【思路】
既然与0号在同一阵营的人都是嫌疑犯,那么这些嫌疑犯如果在其他阵营,那么会惹得其他人也变成嫌疑犯,比如在一号阵营里有 2 4,二号阵营里有 1 2,那么在一号阵营里把2赋值成4的父节点,在二号阵营里把1赋值为2的父节点,那么这两个阵营便连在了一起。那么,由此得出,我们只需要把每一个阵营的人分别找到他们的根捆绑在一起,那么可能会有不同阵营的人也被绑在一起。捆绑完之后,for循环遍历一遍,若是谁的根节点和0的根节点一样,那么这个人就成为了人嫌疑犯。
【代码】
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<iostream>#include<map>#include<queue>#include<stack>#define mem(a,b) memset(a,b,sizeof(a))using namespace std;int n,m;int pre[30000+10];int find(int root){ if(root==pre[root]) return root; else return pre[root]=find(pre[root]);}int main(){ while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; for(int i=0; i<=n-1; i++) pre[i]=i; int k; for(int i=0; i<m; i++) { scanf("%d",&k); int s,t; scanf("%d",&s);/*从阵营里拎出第一个,把它赋值为阵营里其他人的父节点,捆绑在一起*/ int fa=find(s); for(int j=1; j<k; j++) { scanf("%d",&t); int fb=find(t); if(fa!=fb) pre[fb]=fa; } } int num=0; for(int i=0; i<n; i++) if(find(i)==find(0)) num++; printf("%d\n",num); }}
0 0
- poj1611 The Suspects(并查集)
- POJ1611 The Suspects (并查集)
- The Suspects 1611 (并查集)
- The Suspects(并查集问题)
- The Suspects(并查集)
- poj The Suspects(并查集)
- POj1611:The Suspects(并查集)
- POJ_1611-The Suspects(并查集)
- The Suspects (并查集)
- 【POJ1611】The Suspects(并查集)
- The Suspects (并查集)
- The Suspects(并查集)
- poj1611 the suspects(并查集)
- POJ1611 The Suspects(并查集)
- The Suspects(POJ 1611)(裸并查集)
- The Suspects (并查集(裸))
- 【POJ】The Suspects(裸并查集)
- zoj 1789 || poj 1611 The Suspects(并查集)
- 进程以及task_struct结构体
- OpenCV 3.1 imwrite()函数写入异常问题解决方法
- CMake最好的学习资料
- 1625: [Usaco2007 Dec]宝石手镯
- GitHub windows客户端简单使用教程
- The Suspects (并查集(裸))
- 动态生成Swiper效果
- 用slide做html5的ppt
- Android之利用正则表达式校验邮箱、手机号、密码、身份证号码等
- 杨梅坑之旅
- windows封装pthread
- <操作系统:精髓与设计原理> 单处理器调度 多处理器调度 实时调度
- JDBC链接MySQL的一个相对完美的小程序
- JS的三种输出方式