POJ1611 The Suspects 并查集
来源:互联网 发布:淘宝店铺宝贝链接提取 编辑:程序博客网 时间:2024/05/22 10:46
题目大意:有n个学生,分成m组,SARA病毒感染编号为0的学生,某一组学生要被隔离,当且仅当该组里有至少一个人感染SARA病毒,现在问你一共需要隔离多少学生。
典型的并查集问题,找出编号为0的学生所在的集合有多少元素即可。
#include <cstdio>#include <iostream>using namespace std;#define MAX 30010int par[MAX],rank[MAX];void Init(int n){ for(int i=0;i<n;i++) { par[i]=i; rank[i]=1; }}int Find(int x){ if(par[x]!=x) return par[x]=Find(par[x]); return par[x];}void Union(int x,int y){ x=Find(x); y=Find(y); if(x==y) return ; if(rank[x]>rank[y]) { par[y]=x; rank[x]+=rank[y]; } else { par[x]=y; rank[y]+=rank[x]; }}int main(){ int n,m,i,k; int ans,tmp; while(scanf("%d%d",&n,&m)) { if(m==0&&n==0) break; Init(n); while(m--) { scanf("%d%d",&k,&ans); for(i=1;i<k;i++) { scanf("%d",&tmp); Union(ans,tmp); } } printf("%d\n",rank[Find(0)]); } return 0;}
0 0
- POJ1611 The Suspects 并查集求解
- POJ1611 The Suspects 并查集
- poj1611 The Suspects (并查集)
- poj1611-The Suspects--并查集
- poj1611 The Suspects(并查集)
- POJ1611 The Suspects (并查集)
- 并查集 poj1611 The Suspects
- POJ1611 The Suspects 并查集
- poj1611 The Suspects(基础并查集)
- poj1611 The Suspects 并查集
- POJ1611:The Suspects【并查集】
- poj1611 The suspects 并查集
- 【并查集】:poj1611,The Suspects
- POJ1611 The Suspects(并查集)
- poj1611 the-suspects 并查集
- POj1611:The Suspects(并查集)
- POJ1611 The Suspects 种类并查集
- POJ1611 The Suspects(并查集)
- 一些网站地址
- UVA - 11044 Searching for Nessy
- 固定导航栏css样式待代码
- Java异常处理机制
- OpenNMS编译,打包并在Windows下启动
- POJ1611 The Suspects 并查集
- oracle11g 常用函数(Functions)详解
- wireshark过滤规则总结
- Mediastreamer2入门需知--数据结构和常用函数
- OpenStack详细解读:定义,好处与使用实例(1)
- UIScrollView的属性总结
- 如何从一个类的ViewController,如何获取一个UIView上的某种控件
- mediastreamer2 的简介
- CxGrid导出到Excel(Delphi)