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 ...
随机推荐
- zabbix监控的配置
ZABBIX监控的操作步骤有两个! 首先登录到zabbix 的主界面在configuration---host---create host在如上的host 创建界面中 主要是输入被监测的server的 ...
- You-Get 一键下载全网视频资源
下载视频 无论是单纯的下载视频收藏,还是以便离线收看,都离不开“下载”,好的工具让你把注意力更好的放在视频的本身,而不用考虑要如何下载视频.下载视频从来不乏方法,之前也介绍了下载 Youtube ...
- rbenv配置
git clone https://github.com/rbenv/rbenv.git ~/.rbenv # 用来编译安装 ruby git clone git://github.com/sstep ...
- NOIP 2008 立体图 (字符串+模拟)
立体图 时间限制: 1 Sec 内存限制: 50 MB提交: 2 解决: 0[提交][状态][讨论版][命题人:外部导入] 题目描述 小渊是个聪明的孩子,他经常会给周围的小朋友们讲些自己认为有趣的 ...
- 迭代器-迭代对象-dir(a)可以查看该数据类型有多少种方法。range(10)在py3里就是一个迭代器,for循环实际就是迭代器的应用
迭代器 我们已经知道,可以直接作用于for循环的数据烈性有以下几种: 一类是集合数据类型,如list.tuple.dict.set.str,bytes等: 一类是generator,数据结构,包括生成 ...
- python开发模块基础:异常处理&hashlib&logging&configparser
一,异常处理 # 异常处理代码 try: f = open('file', 'w') except ValueError: print('请输入一个数字') except Exception as e ...
- linux 混杂设备驱动之adc驱动
linux2.6.30.4中,系统已经自带有了ADC通用驱动文件---arch/arm/plat-s3c24xx/adc.c,它是以平台驱动设备模型的架构来编写的,里面是一些比较通用稳定的代码,但是l ...
- 赋予oracle执行存储过程权限和创建表权限
grant create any table to username; grant create any procedure to username; grant execute any proced ...
- AngryFuzz3r-web扫描工具
项目地址:https://github.com/ihebski/angryFuzzer 下载完后打开文件 安装一下所需的Python模块 root@sch01ar:/sch01ar/angryFuzz ...
- windows Server 2008各版本有何区别?
windows Server 2008有几个版本,先一一列出来把: Windows Server 2008 Standard Edition (标准版) Windows Server 2008 ...