本文出自: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 (并查集)的更多相关文章

  1. POJ1611 The Suspects 并查集模板题

    题目大意:中文题不多说了 题目思路:将每一个可能患病的人纳入同一个集合,然后遍历查找每个点,如果改点点的根节点和0号学生的根节点相同,则该点可能是病人. 模板题并没有思路上的困难,只不过在遍历时需要额 ...

  2. The Suspects(并查集维护根节点信息)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 37090   Accepted: 17980 De ...

  3. poj 1611 The Suspects(并查集输出集合个数)

    Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...

  4. poj 1611 The Suspects 并查集变形题目

    The Suspects   Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 20596   Accepted: 9998 D ...

  5. B - The Suspects(并查集)

    B - The Suspects Time Limit:1000MS     Memory Limit:20000KB     64bit IO Format:%lld & %llu Desc ...

  6. POJ 1611 The Suspects (并查集+数组记录子孙个数 )

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24134   Accepted: 11787 De ...

  7. POJ 1611 The Suspects (并查集求数量)

    Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...

  8. poj1611 带权并查集

    题意:病毒蔓延,现在有 n 个人,其中 0 号被认为可能感染,然后给出多个社交圈,如果某个社交圈里有人被认为可能被感染,那么所有这个社交圈里的人都被认为可能被感染,现在问有多少人可能被感染. 带权并查 ...

  9. POJ 1611 The Suspects 并查集 Union Find

    本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...

  10. poj 1611 The Suspects 并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 30522   Accepted: 14836 De ...

随机推荐

  1. 基于visual Studio2013解决面试题之1307二分查找

     题目

  2. ul不加宽高

    ul可以不加宽高,但是不能用margin(上下左右), 可以用margin(左右),否则里面的内容如果是要左右浮动的话,就会掉下来

  3. Single Image Haze Removal Using Dark Channel Prior翻译

    这段时间在回顾以前做的去雾,顺便就把这篇文章翻译了一下,看看有没有同行交流.由于是用LaTex排版的,所以只能转为图片传上来了.另外,英语水平有限,大部分都是靠词典,所以只能勉强着看了.

  4. MyBatis深入理解一

    Mybatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .iB ...

  5. sum(case when then)(男女生的个数)

    判断类似一个班级的男生和女生的人数,用sum (cese when then ) select count(er.execute_result), sum(case er.execute_result ...

  6. shell 调用mysql 存储过程判断真假

    mysql> create table TBL_STUDENT(id int,name char(10),CLASSNO int,BIRTH datetime); Query OK, 0 row ...

  7. 《学习opencv》笔记——矩阵和图像操作——cvSetIdentity,cvSolve,cvSplit,cvSub,cvSubS and cvSubRS

    矩阵和图像的操作 (1)cvSetIdentity函数 其结构 void cvSetIdentity(//将矩阵行与列相等的元素置为1.其余元素置为0 CvArr* arr//目标矩阵 ); 实例代码 ...

  8. Swift - 程序进入后台,以及应用终止时调用的方法

    在AppDelegate中有如下两个方法要注意: applicationDidEnterBackground()  当应用进入后台时起作用 applicationWillTerminate()  当应 ...

  9. 基于visual Studio2013解决面试题之0405和最大的子矩阵

     题目

  10. Redis C客户端API - God's blog - 博客频道 - CSDN.NET

    Redis C客户端API - God's blog - 博客频道 - CSDN.NET Redis安装步骤: 1.redis server安装 wget http://redis.googlecod ...