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. Linux centosVMware Nginx访问日志、Nginx日志切割、静态文件不记录日志和过期时间

    一.Nginx访问日志 vim /usr/local/nginx/conf/nginx.conf //搜索log_format  日至格式 改为davery格式 $remote_addr  客户端IP ...

  2. 常用的sql指令

    一,关于数据库中表的一些介绍 1,表的每一行表示关系的一个元组,又称记录: 2,表的每一列表示关系的一个字段,又称属性(例如:id.name.password........): 3,  表结构:表的 ...

  3. JS中,跨域调用(本地)另一个项目的方法

    IP地址,因为是本地的项目,所以我一开始写的是127.0.0.1...,但不对.应该写本机的IP地址才对!

  4. webpack配置文件里loader的执行顺序:从下到上,从右到左; css-loader开启css模块化modules: true,

    注释: options:{ importLoaders: 2 } 解决样式文件里使用@import 'xxx.xxx' 的问题 module: { rules: [{ test: /\.scss$/, ...

  5. 使用oracle 的 PL/Sql 定时执行一个存储过程

    CSDN日报20170322--<关于软件研发的一些体会总结> 同步博客至 CSDN ,让更多开发者看到你的文章 看微博技术大咖解析互联网应用架构实战 使用oracle 的 PL/Sql ...

  6. Hive的存储和MapReduce处理——数据清洗

    日期:2019.11.13 博客期:115 星期三 Result文件数据说明: Ip:106.39.41.166,(城市) Date:10/Nov/2016:00:01:02 +0800,(日期) D ...

  7. importlib 与__import__的区别

    importlib 与__import__的区别 importlib import importlib name = "lib.test" # lib.test指的是lib路劲下的 ...

  8. Linux进程通信方式

    参考:https://www.cnblogs.com/yangykaifa/p/7295863.html

  9. java#tostring

    通常使用apache-commons 来生成tostring方法,但是对于类型为java.util.Date的字段打印效果并不是我们想要的. @Override public String toStr ...

  10. Java中的输入"scanf",Scanner用法

    1.输入程序 1)Scnner的初始用法: 1 import java.util.Scanner; 2 public class Textone { 3 public static void main ...