poj 1611 :The Suspects经典的并查集题目
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
Input
The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
For each case, output the number of suspects in one line.
Sample Input
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
Sample Output
4
1
1
Source
提供两个模板:
模板一:
int ufs[MAXN]; //并查集 void Init(int n) //初始化
{
int i;
for(i=;i<n;i++){
ufs[i] = i;
}
} int GetRoot(int a) //获得a的根节点。路径压缩
{
if(ufs[a]!=a){ //没找到根节点
ufs[a] = GetRoot(ufs[a]);
}
return ufs[a];
} void Merge(int a,int b) //合并a和b的集合
{
ufs[GetRoot(b)] = GetRoot(a);
} bool Query(int a,int b) //查询a和b是否在同一集合
{
return GetRoot(a)==GetRoot(b);
}
模板二:
int pre[];
int find(int x){//查找x父节点
int r=x; //委托r去找父节点
while(pre[r]!=r) //如果r的上级不是r自己(也就是说找到的节点他不是父节点 )
r=pre[r]; // r 接着找他的上级,直到找到父节点 为止
return r;//父节点驾到~~~
}
void join(int x,int y){ //我想让x节点和节点连成一条线
int fx=find(x),fy=find(y);//寻找x,y的父节点
if(fx!=fy)//x和y的父节点显然不是同一个
pre[fx]=fy;//让x的父节点成为y的子节点
}
题意:
有n个学生属于m个团体,(0<n<=30000,0<=m<=500)一个学生可以属于多个团体。一个学生疑似患病,则他属于整个团体都疑似患病,已知0号学生疑似患病,求一共多少个学生患病。
思路:
很经典的并查集的题目,找一个pre[]数组记录存储每一个以当前下标为根节点的集合的个体数目,最后输出0号的根节点对应的sum值,就是0号学生所在团体的人数。
代码:
#include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
using namespace std;
int pre[];
int find(int x){//查找x父节点
int r=x; //委托r去找父节点
while(pre[r]!=r) //如果r的上级不是r自己(也就是说找到的节点他不是父节点 )
r=pre[r]; // r 接着找他的上级,直到找到父节点 为止
return r;//父节点驾到~~~
}
void join(int x,int y){ //我想让x节点和节点连成一条线
int fx=find(x),fy=find(y);//寻找x,y的父节点
if(fx!=fy)//x和y的父节点显然不是同一个
pre[fx]=fy;//让x的父节点成为y的字节点
}
int main(){
std::ios::sync_with_stdio(false);
int n,m;
while(cin>>n>>m){
memset(pre,,sizeof(pre));
if(n==&&m==)break;
for(int i=;i<n;i++) pre[i] = i;
while(m--){
int t, a,b;
cin>>t>>a;
for(int i=;i<t;i++){
cin>>b;
join(a,b);
a=b;
}
}
int sum=;
for(int i=;i<n;i++){
if(find(i)==find())
sum++;
}
cout<<sum+<<endl;
}
return ;
}
poj 1611 :The Suspects经典的并查集题目的更多相关文章
- POJ 1611 The Suspects(简单并查集)
( ̄▽ ̄)" #include<iostream> #include<cstdio> using namespace std; ]; void makeSet(int ...
- 【POJ】The Suspects(裸并查集)
并查集的模板题,为了避免麻烦,合并的时候根节点大的合并到小的结点. #include<cstdio> #include<algorithm> using namespace s ...
- poj 1611:The Suspects(并查集,经典题)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21472 Accepted: 10393 De ...
- 并查集 (poj 1611 The Suspects)
原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
- POJ 1611 The Suspects (并查集)
The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...
- poj 1611 The Suspects(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 De ...
- poj 1611 The Suspects(并查集输出集合个数)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj 1611 The Suspects 并查集变形题目
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 20596 Accepted: 9998 D ...
随机推荐
- 03_TypeScript函数
1.函数的定义 es5定义函数的方法 //函数声明法 function run(){ return 'run'; } //函数表达式 var run = function(){ return 'run ...
- C#使用OracleBulkCopy
首先使用PL/SQL 通过语句:select * from v$version; 查询出使用的oracle版本,弄到对应版本的Oracle.DataAccess.DLL 我本地使用版本为:11.2. ...
- 洛谷P1765 手机_NOI导刊2010普及(10) 关于cin和getline的一些区别 以及一些STL
一. cin>>s:cin>>是由两部分构成的,cin和>>,其中cin是输入流istream类的一个对象,隶属于iostream函数库而>>则是运算符 ...
- PP: Multilevel wavelet decomposition network for interpretable time series analysis
Problem: the important frequency information is lack of effective modelling. ?? what is frequency in ...
- 占位 SC
占位 SC include: SC404 SC405
- HTML连载63-a标签的伪类选择器
一.a标签的伪类选择器 1.通过观察可以发现a标签存在一定状态 (1)默认状态,从未被访问过 (2)被访问过的状态 (3)鼠标长按的状态 (4)鼠标悬停在a标签上的演示 2.什么是a标签的伪类选择器? ...
- Your wechat account may be LIMITED to log in WEB wechat, error info: <error><ret>1203</ret><message>为了你的帐号安全,此微信号不能登录网页微信。你可以使用Windows微信或Mac微信在电脑端登录。Windows微信下载地址:WeChat for PC
转载:https://zhuanlan.zhihu.com/p/76180564 微信网页版限制登录或禁止登录将影响一大批使用itchat等Web Api方案的微信机器人 网页版微信 API 被封了, ...
- git push出错的解决办法
今天push代码到线上的时候怎么都不行,尝试了很多办法报了好几种错比如: 反正就是各种错,然后其实不管什么错,你全部Git init 一下然后重新配置 git config --global user ...
- CodeForces 1141B
https://vjudge.net/problem/CodeForces-1141B #include<bits/stdc++.h> using namespace std; int m ...
- 腾讯短链接url生成接口/腾讯短网址在线生成/新浪微博短链接生成器的分享
在通常情况下,URL是由系统生成的,通常包括URI路径,多个查询参数,可以对参数进行加密和解密. 当人们要分享某个URL,比如短信,邮件,社交媒体,这就需要短URL.而短网址,顾名思义就是在长度上比较 ...