The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 28293   Accepted: 13787

Description

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

题意是已知编号为0的同学已经染上了SARS病毒,然后跟他一个组的同学假设也染上了SARS病毒,问最后有多少个同学染上了SARS病毒。

并查集模板题,一开始以为要用什么方法找0的孩子数量,后来发现枚举时间也不会卡。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <set>
#pragma warning(disable:4996)
using namespace std; int n,m;
int pre[30002]; int findpre(int x)
{
while(x!=pre[x])
{
x=pre[x];
}
return x;
} void union_set(int x,int y)
{
int pre_x = findpre(x);
int pre_y = findpre(y); if(pre_x == pre_y)
return;
else if(pre_x>pre_y)
{
int temp = pre_x;
pre_x = pre_y;
pre_y = temp;
} pre[pre_y]=pre_x;
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i,j,num,temp1,temp;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
{
break;
}
for(i=0;i<n;i++)
{
pre[i]=i;
}
for(i=1;i<=m;i++)
{
scanf("%d",&num);
for(j=1;j<=num;j++)
{
if(j==1)
{
scanf("%d",&temp1);
continue;
}
else
{
scanf("%d",&temp);
union_set(temp,temp1);
}
}
}
int ans=0;
for(i=0;i<n;i++)
{
if(findpre(i)==0)
{
ans++;
}
}
printf("%d\n",ans);
} //system("pause");
return 0;
}

POJ2524题意是查询最终的集合数量,找pre,查有多少不同即可。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <set>
#pragma warning(disable:4996)
using namespace std; int n,m;
int pre[50005];
set <int> res; int findpre(int x)
{
while(x!=pre[x])
{
x=pre[x];
}
return x;
} void union_set(int x,int y)
{
int pre_x = findpre(x);
int pre_y = findpre(y); if(pre_x == pre_y)
return;
else if(pre_x>pre_y)
{
int temp = pre_x;
pre_x = pre_y;
pre_y = temp;
} pre[pre_y]=pre_x;
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i,j,temp1,temp2,test_num; test_num=1;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0 && m==0)
{
break;
} printf("Case %d: ",test_num++); for(i=1;i<=n;i++)
{
pre[i]=i;
}
for(i=1;i<=m;i++)
{
scanf("%d%d",&temp1,&temp2);
union_set(temp1,temp2);
}
res.clear();
for(i=1;i<=n;i++)
{
res.insert(findpre(i));
}
printf("%d\n",res.size());
} //system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ1611 && POJ2524 并查集入门的更多相关文章

  1. hdu1272并查集入门

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  2. 并查集入门--畅通工程(HDU1232)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 畅通工程 Time Limit: 4000/2000 MS (Java/Others)    M ...

  3. poj1611(并查集)

    题目链接:http://poj.org/problem?id=1611 题意: SARS(非典型肺炎)传播得非常厉害,其中最有效的办法是隔离那些患病.和患病者接触的人.现在有几个学习小组,每小组有几个 ...

  4. poj1611 简单并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 32781   Accepted: 15902 De ...

  5. poj2524(并查集水题)

    题目链接:http://poj.org/problem?id=2524 题目大意:学校共有n个同学,告诉你m对同学信仰同一宗教,问这个学校学生信仰宗教的数目最多为多少. 例: Sample Input ...

  6. poj1611(并查集简单应用)

    题目链接:http://poj.org/problem?id=1611 思路: 显然是一个并查集的题,很简单,只要将一个group中的学生并在一起,最后遍历1到n-1,看有多少学生的祖先与0的祖先相等 ...

  7. 并查集——poj2492(带权并查集入门)

    一.题目回顾 题目链接:传送门 题意:给定n只虫子,不同性别的可以在一起,相同性别的不能在一起.给你m对虫子,判断中间有没有同性别在一起的. 二.解题思路 种类并查集 和poj1073的本质一样 详见 ...

  8. 并查集——poj1703(带权并查集入门)

    传送门:Find them, Catch them 题意:警察抓获N个罪犯,这些罪犯只可能属于两个团伙中的一个,现在给出M个条件(D a b表示a和b不在同一团伙),对于每一个询问(A a b)确定a ...

  9. HDU1213:How Many Tables(并查集入门)

    -----------刷点水题练习java------------- 题意:给定N点,M边的无向图,问有多少个连通块. 思路:可以搜索;  可以并查集.这里用并查集练习java的数组使用,ans=N, ...

随机推荐

  1. Electromagnetic

    1. 电磁辐射 2. 电磁频谱 3. 可见光 4. 微波 5. 更多相关链接 1. 电磁辐射 https://en.wikipedia.org/wiki/Electromagnetic_radiati ...

  2. iframe结构的网站按F5刷新子页面的实现方式

    有的网站或者后台系统由于页面有公共的部分,比如菜单,会把公共的部分放在一个页面,这里称之为父页面,而把具体的内容放入一个iframe中,之后的请求改变iframe的内容.但是这样会有一个问题,因为浏览 ...

  3. springboot内置的定时任务简单使用

    直接上图:搞定(一定要加@EnableScheduling(开启定时任务)这个注解@Component(让spring扫描到)),下面是每五秒执行一次 结果:

  4. Java最新面试题

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  5. Eclipse传递main函数参数

    在项目上右击 Run As->Run Configurations...->Arguments->在Program arguments:的文本框中输入你要传入的参数,若有几个参数则在 ...

  6. (转)jquery.validate插件的使用

    JQuery Validate使用总结:一.导入js库<script src="../js/jquery.js" type="text/javascript&quo ...

  7. nginx 打印详细请求

    log_format main escape=json '{ "@timestamp": "$time_iso8601", ' '"remote_ad ...

  8. Netty实现原理和使用

    参考: https://www.jdon.com/concurrent/netty.html Java NIO原理和使用 参考:https://www.jdon.com/concurrent/nio% ...

  9. Day3-N - Monthly Expense POJ3273

    Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...

  10. Ternsorflow 学习:000-在 Ubuntu 16.04 上安装并使用 TensorFlow_v1.14 (改)

    声明:本人已经对原文链接根据情况做出合理的改动. 本系列文章使用的是Tensorflow v1.14 引言 TensorFlow 是由谷歌构建的用于训练神经网络的开源机器学习软件.TensorFlow ...