The Suspects poj 1611 并查集
来源:互联网 发布:淘宝不清洗会怎么样 编辑:程序博客网 时间:2024/06/15 21:38
传送门:poj 1611
题意:有一群人和很多组,每个人可能属于很多组,0号患了传染病,和他一组的人都被列为怀疑对象,和怀疑对象一组的也是怀疑对象。
并查集维护就好了,需要注意的是并查集中0号所在的树必定要0号当主根,这样最后才好统计人数。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int f[30005];int getf(int k){return k==f[k]?k:f[k]=getf(f[k]);}int a[30005];int main(){int n,m;while(scanf("%d%d",&n,&m)&&m+n){int t;for(int i=0;i<n;i++)f[i]=i;for(int i=0;i<m;i++){scanf("%d",&t);for(int j=0;j<t;j++){scanf("%d",&a[j]);if(j>0){if(getf(a[j-1])==0)//如果前一个结点的根是0号,那么就将当前点向左归并进去。f[getf(a[j])]=getf(a[j-1]);elsef[getf(a[j-1])]=getf(a[j]);//否则的话就一直让右边的点当主根,因为前面没有0,那么0只有可能在右边。}}}int ans=0;for(int i=0;i<n;i++)if(getf(i)==0){//printf("%d ",i);ans++;}printf("%d\n",ans);}return 0;}
0 0
- POJ 1611 The Suspects(并查集)
- poj 1611 The Suspects 并查集
- POJ 1611 The Suspects 并查集
- poj 1611 The Suspects(并查集)
- POJ 1611 The Suspects 并查集
- POJ--1611--The Suspects【并查集】
- poj 1611-The Suspects:并查集
- POJ--1611 -- The Suspects [并查集]
- poj 1611 - The Suspects(并查集)
- poj 1611The Suspects 并查集
- poj 1611 The Suspects 并查集
- [并查集]POJ 1611 The Suspects
- poj 1611 The Suspects --- 并查集
- POJ 1611 The Suspects (并查集)
- POJ 1611 The Suspects(并查集)
- POJ 1611--The Suspects 并查集
- POJ 1611 The Suspects 并查集
- poj 1611 The Suspects(并查集)
- jzoj4887 最大匹配
- python中运算符and、or、not
- Eclipse中免插件使用javafx库
- 本文无用,新建试用
- usaco1.3.2 Barn Repair
- The Suspects poj 1611 并查集
- warning: LF will be replaced by CRLF
- 圆有点挤
- 贪心算法学习总结
- Eclipse 和MyEclipse的关系
- go defer
- 关于UrlRewrite的使用
- JSP页面跳转大全
- 微信分享的原生集成