原创


The Suspects

Time Limit: 1000MS  Memory Limit: 20000K

Total Submissions: 48698  Accepted: 23286

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

Source

 
此题出自POJ——1611:http://poj.org/problem?id=1611
 
题目大意:
  学校要找出患了SARS一共有多少人,n个学生人数(编号0~n-1),m个协会,每个协会有k个人,并给出这k个人的编号。
每个学生可以参加多个协会,如果一个协会里面存在1个或多个患病学生,则协会全部人都会患病,所以患病协会里面可能
存在参加多个协会的患病学生,这样会带来协会传染,学校要求求出患病人数,当然,定义编号为0的学生是首个患病学生。
  输入:第一行n(学生人数)和m(协会数),接下来m行,每行第一个数代表k,接下来k个数代表参加此协会的学生的编号,
  n==m==0时表示结束。
  输出:患病学生人数
注意:可以输入多组数据,等结束输入后再一起分行输出结果。
 
解题思路:(并查集算法具体思路不再赘述,详情查看:https://www.cnblogs.com/chiweiming/p/9310599.html
  并查集,我们把每个协会的学生的编号整合起来放在同一棵树上,若一个学生参加了多个协会,则他所参协会的多棵树会被合并起来成为一棵,这样最后只需要找出编号为0的学生所在的树的结点数就是患病学生的人数了。
  值得注意的是,代码中对寻找根节点的算法进行了优化!
  原来只是单纯的寻找某个结点所在树的根节点,并没有改变数的结构:
  return node==student[node]?node:find_Father(student[node]); 

  优化后,改变了树的结构,将结点至根节点的链上的所有结点直接指向了根节点,大大方便了下次再次搜寻的效率!

  static int find_Father(int node) {    //寻找根节点
/*
return node==student[node]?node:find_Father(student[node]);
*/ if(node!=student[node]) {
student[node]=find_Father(student[node]);
//状态压缩,将结点node到根节点这条链上的结点直接指向根节点,优化下次寻找根节点的时间
}
return student[node]; }

Java代码:

import java.util.*;

public class TheSuspects {

    static int n;    //学生数量
static int m; //组的数量
static int student[];
static int book[]; static int find_Father(int node) { //寻找根节点
/*
return node==student[node]?node:find_Father(student[node]);
*/ if(node!=student[node]) {
student[node]=find_Father(student[node]);
//状态压缩,将结点node到根节点这条链上的结点直接指向根节点,优化下次寻找根节点的时间
}
return student[node]; } public static void main(String[] args) { Scanner reader=new Scanner(System.in);
ArrayList list=new ArrayList();
int count=0; while(reader.hasNext()) { n=reader.nextInt();
m=reader.nextInt();
if(n==0 && m==0) {
break;
}
student=new int[n];
book=new int[n];
for(int i=0;i<n;i++) {
student[i]=i; //学生指向自己
book[i]=1; //每个学生为1个结点
}
for(int i=1;i<=m;i++) {
int k=reader.nextInt();
int node_One=reader.nextInt(); //输入k个结点中的第一个
for(int j=1;j<=k-1;j++) {
int node_Two=reader.nextInt();
int father_One=find_Father(node_One); //找出父节点
int father_Two=find_Father(node_Two);
if(father_One!=father_Two) {
student[father_Two]=father_One; //合并
book[father_One]+=book[father_Two]; //book[根节点]存储其所在树的结点数,每有一个学生加入此数,总结点数+1
}
}
}
list.add(book[find_Father(0)]); //求0所在树的结点个数
count++; }
for(int i=0;i<count;i++) {
System.out.println(list.get(i));
}
} }

POJ截图:

23:31:36

2018-07-18

The Suspects——Asia Kaohsiung 2003的更多相关文章

  1. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

  2. poj 1611:The Suspects(并查集,经典题)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 De ...

  3. poj 1611 The Suspects(并查集)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21598   Accepted: 10461 De ...

  4. 【原创】poj ----- 1611 The Suspects 解题报告

    题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS   Memory Limit: 20000K To ...

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

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

  6. 并查集模板题(The Suspects )HZNU寒假集训

    The Suspects Time Limit: 1000MS Memory Limit: 20000KTotal Submissions: 36817 Accepted: 17860 Descrip ...

  7. [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21586   Accepted: 10456 De ...

  8. poj1611---The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 19754   Accepted: 9576 Des ...

  9. POJ----The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 18890   Accepted: 9150 Des ...

随机推荐

  1. mysql各种集群的优缺点

    mysql各种集群的优缺点 1.主从架构:只是有数据备份的功能: 2.主主互备+keepalived:实现数据备份加高可用: 3.主主互备,主主下面分别挂个从: 4.A和B主主互备,把从库都挂到B下, ...

  2. LVS+Keepalived搭建

    LVS+Keepalived搭建 原理说明 (推荐): http://www.cnblogs.com/likehua/archive/2014/06/19/3796849.html http://ou ...

  3. Java基础知识复习(二)

    Java 重写(Override)与重载(Overload) 重写 是子类对父类的允许访问的方法的实现过程进行重新编写,返回值和形参都不能改变,属于编译时多态.即外壳不变,核心重写! 重写的好处在于子 ...

  4. laravel中session的过期时间

    在项目开发的过程中,前后端分离 需要用session保存用户的登陆信息 这就涉及到session的有效期了 session又分为php中的session有效期和laravel中的session的有效期 ...

  5. 数据科学:numpy.where() 的用法

    原文出处:numpy.where() 用法讲解 原创作者:massquantity numpy.where() 有两种用法: 1. np.where(condition, x, y) 满足条件(con ...

  6. Java-Maven-Runoob:Maven 构建 Java 项目

    ylbtech-Java-Maven-Runoob:Maven 构建 Java 项目 1.返回顶部 1. Maven 构建 Java 项目 Maven 使用原型 archetype 插件创建项目.要创 ...

  7. 关于ESXI能虚拟出多少个虚拟机和CPU的关系

    当你的虚拟机报如下错误的时候: esxi5.0版本最高配置: https://www.vmware.com/content/dam/digitalmarketing/vmware/zh-cn/pdf/ ...

  8. Struts2接受页面传值过程中出现input的问题

    其实我在使用Struts2的时候,遇到要求返回input的时候不算少.一般我们在使用Struts2的时候,都会返回SUCCESS/ERROR,或者是NONE以到Strtuts的配置文件中再进行相应的处 ...

  9. Java学习之Mysql结构优化

    背景:业务发展初期为了便于快速迭代,很多应用都采用集中式的架构,随着业务规模的扩展,系统变得越来越复杂,访问量越来越大,不得不进一步扩展系统的吞吐能力. 优化1.主从集群:通过数据库的复制策略,可以将 ...

  10. oracle 查询中实现分页

    那么Oracle如何实现分页呢?--Oracle分页查询SELECT   * FROM   (     SELECT         ROWNUM R,YANGCQ_ID,YANGCQ_BRANCHI ...