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 ...
随机推荐
- 操作系统的页面置换C++算法:OPT FIFO LRU CLOCK 计算缺页率
暴力直接上代码,主要是用了vector来实现,有些方法比較费时,不太好,请各位大神斧正.这是个人的作业, 这是代码下载页http://download.csdn.net/detail/l631068 ...
- UVa 657 掷骰子
意甲冠军:有一个大图.每个像素是格孩子只可能是 . * X 三种.代表背景.玻色子.色子点. 两格子是邻近或在通信,当且仅当两个格儿子*要么X.且具有共同的边,这是上下左右四个方向,斜过,即四连块. ...
- QT中的pro文件的编写
原地址:http://blog.csdn.net/fjb2080/article/details/4833666 我们在编译QT的工程的时候,一般都会让qmake自动生成,但有时我们需要定制我们的工程 ...
- WCF随笔3----消息编码器
原文:WCF随笔3----消息编码器 我们都知道,message是wcf通信框架进行通信的最基本的单位,但是wcf开发人员其实根本不需要直接与message打交道,一样能够写好wcf相关的程序.这是因 ...
- psl/sql本地与远程连接配置
一:下载Oracleclient 下载地址:http://www.oracle.com/technetwork/database/features/instant-client/index-09748 ...
- Keepalived安装工具
装keepalived前,要先检查主机上是否已经安装, ps -ef | grep keepalive 不检查的话.easy把前人装的东西覆盖掉,那么曾经弄的配置文件都没了比較麻烦. 下面都为root ...
- C# - 使用 List<> 泛型给GridView控件数据
创建实体模型: namespace Test.Models { public class Student { public string ID { get; set; } public string ...
- Q10Ⅱ 双核 - 产品中心 - 海美迪
海美迪Q系列视频文明书 Q10Ⅱ 双核 - 产品中心 - 海美迪
- 《生活在Linux中》之:使用Bash就是使用Emacs
定义bash Emacs模式下的快捷键请参考: Readline-在BASH下自定义键盘热键 未完待续...
- Ceph与GlusterFS等等(80篇博客)
http://blog.csdn.net/liuben/article/category/373751