poj1611 The Suspects
来源:互联网 发布:pubmed数据库 编辑:程序博客网 时间:2024/05/20 21:44
题目链接:http://poj.org/problem?id=1611
题意:有n个人,总共有m个社团,现在0号病了,如果和0号处于一个社团的就会一起生病,总之在社团之间会相互传染,比如样例1,现在问你有几个人生病了
解析:并查集,对于全部人来做并查集,社团之间相互有连接就加到一个集合里面,最后看0号结点的那个集合有多少个元素即可,记录元素个数,可以开一个son数组,在合并的时候更新一下
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;const int maxn = 1e5+100;int fa[maxn],son[maxn];int getfa(int x){ if(fa[x]==x) return fa[x]; return fa[x] = getfa(fa[x]);}void merge(int u,int v){ int t1 = getfa(u); int t2 = getfa(v); if(t1!=t2) { fa[t2] = t1; son[t1] += son[t2]; }}int main(void){ int n,m; while(~scanf("%d %d",&n,&m)) { if(n==0 && m==0) break; for(int i=0;i<n;i++) { fa[i] = i; son[i] = 1; } for(int i=0;i<m;i++) { int k,t1,t2; scanf("%d %d",&k,&t1); for(int j=1;j<k;j++) { scanf("%d",&t2); merge(t1,t2); t1 = t2; } } printf("%d\n",son[getfa(0)]); } return 0;}
阅读全文
0 0
- The Suspects POJ1611
- POJ1611:The Suspects
- POJ1611 The Suspects
- poj1611 The Suspects
- POJ1611 The Suspects
- POJ1611:The Suspects
- POJ1611-The Suspects-ACM
- POJ1611---The Suspects
- poj1611 - The Suspects
- poj1611 The Suspects
- 【POJ1611】The Suspects
- poj1611 The Suspects
- POJ1611 The Suspects
- The Suspects<poj1611>
- poj1611 -- The Suspects
- POJ1611-The Suspects
- poj1611 - The Suspects
- POJ1611:The Suspects
- Linux控制台(console)和图形化界面之间的切换
- git认证证书问题解决方案(github clone "Permission denied (publickey). fatal: Could not read from remote reposi)
- 优先级队列 + 哈夫曼树
- Fred 7.0.0 光线追迹软件_USB加密锁,全模块运行稳定版\
- 如何在双击后运行shell脚本
- poj1611 The Suspects
- Linux 星状SSH免密脚本
- 剑指offer中连续子数组的最大和+最大的子序列和的问题
- JavaScript框架对比
- 微积分的历史演变与现状
- 什么是CS和BS结构,两种结构的区别
- 将你的 CentOS 变成 OSPF 路由器
- Python单链表逆置
- 进行Linux性能监控的命令介绍