POJ1611 The Suspects (并查集)
本文出自:http://blog.csdn.net/svitter
题意:0号学生染病,有n个学生,m个小组。和0号学生同组的学生染病,病能够传染。
输入格式:n,m
数量 学生编号1,2,3,4
//m个分组
题解:最为典型的并查集。
解法一:求出全部的集合,然后求出0的parent,把parent为0的parent全部学生求出。
解法二:在计算的过程中统计total;
解法一:(一開始用的Vim写的在POJ上交出现 訪问禁止错误- - 不知道又奇妙的碰上了什么BUG,删了main函数中几个换行符就好了)
#include <iostream>
#include <stdio.h>
using namespace std; int stu[30001];
//the num of stu, group
int n, m; void init()
{
for(int i = 0; i < n; i++)
stu[i] = i;
} int getParent(int a)
{
if(a == stu[a])
return a;
else
return stu[a] = getParent(stu[a]);
} void Merge(int a, int b)
{
if(getParent(a) == getParent(b))
return; else
stu[getParent(a)] = b;
} int main()
{
int i, j;
int deter, sum;
int num;
int temp1, temp2; while(scanf("%d%d", &n, &m))
{
if(n == 0 && m == 0)
break;
init();
for(i = 0; i < m; i++)
{
scanf("%d", &num);
scanf("%d", &temp1);
for(j = 1; j < num; j++)
{
scanf("%d", &temp2);
Merge(temp1, temp2);
}
}
deter = stu[getParent(0)];
sum = 1;
for(i = 1; i < n; i++)
{
if(getParent(i) == deter)
sum++;
} printf("%d\n", sum);
}
return 0;
}
解法二:
#include <iostream>
#include <stdio.h>
using namespace std; int stu[30001];
int total[30001];
//the num of stu, group
int n, m; void init()
{
for(int i = 0; i < n; i++)
{
stu[i] = i;
total[i] = 1;
}
} int getParent(int a)
{
if(a == stu[a])
return a;
else
return stu[a] = getParent(stu[a]);
} void merge(int a, int b)
{
if(getParent(a) == getParent(b))
return; else
{
total[getParent(a)] += total[getParent(b)];
stu[getParent(b)] = a;
}
} int main()
{
int i, j;
int num;//record the group's num
int temp1, temp2; while(scanf("%d%d", &n, &m))
{
if(n == 0 && m == 0)
break;
init();
for(i = 0; i < m; i++)
{
scanf("%d", &num);
scanf("%d", &temp1);
for(j = 1; j < num; j++)
{
scanf("%d", &temp2);
merge(temp1, temp2);
}
} printf("%d\n", total[getParent(0)]); //不能直接stu[0],由于stu[0]不一定是根节点。
}
return 0;
}
POJ1611 The Suspects (并查集)的更多相关文章
- POJ1611 The Suspects 并查集模板题
题目大意:中文题不多说了 题目思路:将每一个可能患病的人纳入同一个集合,然后遍历查找每个点,如果改点点的根节点和0号学生的根节点相同,则该点可能是病人. 模板题并没有思路上的困难,只不过在遍历时需要额 ...
- The Suspects(并查集维护根节点信息)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 37090 Accepted: 17980 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 ...
- B - The Suspects(并查集)
B - The Suspects Time Limit:1000MS Memory Limit:20000KB 64bit IO Format:%lld & %llu Desc ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
- POJ 1611 The Suspects (并查集求数量)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj1611 带权并查集
题意:病毒蔓延,现在有 n 个人,其中 0 号被认为可能感染,然后给出多个社交圈,如果某个社交圈里有人被认为可能被感染,那么所有这个社交圈里的人都被认为可能被感染,现在问有多少人可能被感染. 带权并查 ...
- POJ 1611 The Suspects 并查集 Union Find
本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...
- poj 1611 The Suspects 并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30522 Accepted: 14836 De ...
随机推荐
- QNX 线程 调度策略 优先级 时钟频率 同步
/* * barrier1.c */ #include <stdio.h>#include <unistd.h>#include <stdlib.h>#includ ...
- CLR和.Net对象
CLR和.Net对象生存周期 前言 1. 基础概念明晰* 1.1 公告语言运行时* 1.2 托管模块* 1.3 对象和类型* 1.4 垃圾回收器 2. 垃圾回收模型* 2.1 为什么需要垃圾回收* 2 ...
- Java自注三进入
由于近期学的内容实际操作比較多,所以新的笔记就用代码为主体吧! 本回主要内容是输入,Java中主要用Scanner类和BufferedReader.整体来说不难,但有些细节能够总结,看代码: impo ...
- 九道大型软件公司.net面试题!一定得看(附答案)
1:a=10,b=15,在不用第三方变量的前提下,把a,b的值互换 2:已知数组int[] max={6,5,2,9,7,4,0};用快速排序算法按降序对其进行排列,并返回数组 3:请简述面向 ...
- JS - 鼠标经过边框旋转
*右侧为鼠标经过时效果. 下载地址:http://www.lanrentuku.com/js/tupian-1200.html
- [置顶] 手把手教你iOS消息推送证书生成以及Push消息
iOS推送消息是许多iOS应用都具备的功能,今天在给应用加推送功能,在生成证书的过程中,发生了各种令人蛋痛的事.下面就把步骤拿出来分享下: iOS消息推送的工作机制可以简单的用下图来概括: Provi ...
- UML之九图概述
最近看了UML的九种图的讲解,这九种图在我们以后的学习中起着举足轻重的作用,不管是在写文档,还是在对系统的需求.设计进行分析时,都很重要,所以首先做一下概述,希望能和大家分享. 首先和大家展示一下我对 ...
- Metasploit学习之msf连接数据库
kali使用metasploit开启数据服务: 首先,初次使用系统要初始化建立数据库msf3, 否则的话 /opt/metasploit/apps/pro/ui/config/databse.yml不 ...
- ios23-文件上传
1.上传图片 3. // // ios23_uploadViewController.h // ios23-upload // // Created by on 13-6-17. // Co ...
- keil uVision4的安装以及KEIL_Lic.exe的注冊
1.首先毋庸置疑,在网上下载keil uVision4的EXE可运行文件,可能存在两个版本号.51核的单片机(33.3M)和微控制器开发合集(244M),可依据自己的实际须要选择.没有必要都装 2.依 ...