The Suspects——Asia Kaohsiung 2003
原创
The Suspects
Time Limit: 1000MS Memory Limit: 20000K
Total Submissions: 48698 Accepted: 23286
Description
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently,
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
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
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
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的更多相关文章
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
- poj 1611:The Suspects(并查集,经典题)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21472 Accepted: 10393 De ...
- poj 1611 The Suspects(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 De ...
- 【原创】poj ----- 1611 The Suspects 解题报告
题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS Memory Limit: 20000K To ...
- The Suspects(并查集维护根节点信息)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 37090 Accepted: 17980 De ...
- 并查集模板题(The Suspects )HZNU寒假集训
The Suspects Time Limit: 1000MS Memory Limit: 20000KTotal Submissions: 36817 Accepted: 17860 Descrip ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...
- poj1611---The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 19754 Accepted: 9576 Des ...
- POJ----The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 18890 Accepted: 9150 Des ...
随机推荐
- elang和python互通的例子
抄袭自http://www.erlangsir.com/2011/04/14/python-%E5%92%8Cerlang%E4%BA%92%E9%80%9A%E4%BE%8B%E5%AD%90/ t ...
- HTTP重要概念
connection连接 一个传输层的实际环流,它是建立在两个相互通讯的应用程序之间. 在http1.1,request和reponse头中都有可能出现一个connection的头,此header的含 ...
- 解决Maven提示:Could not read settings.xml, assuming default values
本文转载自:http://blog.csdn.net/hqocshheqing/article/details/47702049 最近在学习Maven 时总是出现 Could not read se ...
- java代码练习======每隔5行打印数字
总结:当我们感觉数字排列横排,竖排不好看的时候,学会空几行在排列,哎呦,效果不错喔 package com.aa; public class West2 { public static void ma ...
- 【LigerUI实战】Grid弹出编辑popup
最近为分公司的项目做外援,做起了全栈的活.被公司自研架构磨灭的几乎忘掉了其他技能,正好新项目决定重新搭个架构,与同事们讨论过后,决定Spring+MyBatis+SpringMVC+CXF+Liger ...
- MariaDB存在的问题
MySQL与MariaDB对嵌套的查询语句当中的order by的处理方法不同.MySQL会忠实执行内层查询的排序子句,但是MariaDB会将这个order by去掉,理论依据就是关系理论 --- 一 ...
- Deep Learning 学习笔记(3):Linear Regression 数据的预处理
为了获得良好的收敛,在进行梯度下降前,我们可以对数据进行预处理. 目标是使得数据大小在同一个数据数量级上,均值为零. 一般将数据放缩到(-1,1)区间, 我们可以对数据进行如下操作: 其中u1是数据的 ...
- Oracle使用split和splitstr函数批量分隔字符串
/* * Oracle 创建 split 和 splitstr 函数 */ /* 创建一个表类型 */ ) / /* 创建 split 函数 */ CREATE OR REPLACE FUNCTION ...
- struts2中s:iterator的使用(2个list嵌套循环)
<s:iterator value="packagePlateTbls" id="plateTbls"> <tr> <td cla ...
- 【287】◀▶ arcpy 常用类说明
ArcPy 类列表(按字母顺序) 01 Raster 创建一个可在 Python 脚本或地图代数表达式中使用的栅格对象. 02 Cursor Cursor 是一种数据访问对象,可用于在表中迭代 ...