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 ...
随机推荐
- Android studio: 自 maven 增加一個函式庫
在 android studio 裏要加入一個 3rd party 的 library 其實不是很麻煩, 祇是現在沒有 UI, 所以需要一些手動作業.看來 google 很看好 android stu ...
- 基于visual Studio2013解决面试题之1003字符串逆序
题目
- 基于visual Studio2013解决C语言竞赛题之1039移动
题目 解决代码及点评 /* 39. 有n个整数,编程序将前面的各个数依次向后移动k个位置, 最后k个数移到最前边的k个位置(见下图,其中n=8,k=3). */ # ...
- 程序实践系列(七)C++概述
理论练习题 C++语言与C语言的本质区别是什么? [參考答案]:C++与C语言的本质区别就在于C++是面向对象的.而C语言是面向过程的. 面向过程的程序设计方法与面向对象的程序设计方法在对待数据和函数 ...
- Android SurfaceView实战 打造抽奖转盘
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41722441 ,本文出自:[张鸿洋的博客] 1.概述 今天给大家带来Surfac ...
- 怎样基于谷歌地图的Server缓存公布Image Service服务
怎样基于谷歌地图的Server缓存公布Image Service服务 第一步:下载地图数据 下载安装水经注万能地图下载器,启动时仅仅选择电子.谷歌(这里能够依据自己的须要选择).例如以下图所看到的. ...
- C# 数据访问编码需要遵循的几个规范
一,链接打开之后必须关闭,否则会占用系统空间 SqlConnection conn=new SqlConnection(CONNECTIONSTRING); conn.open(); conn.clo ...
- Vue ES6
Vue ES6 Jade Scss Webpack Gulp 一直以来非常庆幸曾经有翻过<代码大全2>:这使我崎岖编程之路少了很多不必要的坎坷.它在软件工艺的话题中有写到一篇:“首先是 ...
- Ajax技术--考试计时并自动提交试卷
1.概述 在开发网络考试系统时,考试计时并自动提交试卷是必不可少的功能.由于在答卷过程中,试卷不能刷新,所以需要使用Ajax实现无刷新操作.运行本实例,访问准备考试页面index.jsp,在该页面中, ...
- C++中的常对象和常对象成员
常对象 常对象必须在定义对象时就指定对象为常对象. 常对象中的数据成员为常变量且必须要有初始值,如 Time const t1(12,34,36); //定义t1为常对象 这样的话,在所有的场合中,对 ...