Poj1611The Suspects
A - The Suspects
Time Limit: 1000 MS Memory Limit: 20000 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
Description
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
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
//Accepted 641 ms 272 KB C++ 1211 B
#include <iostream>
#include <cstdio> using namespace std;
const int maxn = 30005;
int father[maxn]; void init(int n)
{
for(int i = 0; i < n; ++i)
father[i] = i;
}
///查找一个节点所在的根节点
int serch(int v)
{
if(father[v] == v) return v;
///如果father[v] == v,v就是根,返回v
return serch(father[v]); ///路径压缩快
///否则继续查找根节点,此处是递归
}
///合并集合
void join(int x, int y)
{
int fx = serch(x), fy = serch(y);
if(fx != fy)
father[fx] = fy;
} int is_same(int x, int y)
{
return (serch(x) == serch(y));
} int main()
{
int n, m;
while(scanf("%d %d", &n, &m) != EOF && (n || m))
{
init(n);
int t;
for(int i = 0; i < m; ++i)
{
scanf("%d", &t);
int a, b;
scanf("%d", &a);
t--;
while(t--)
{
scanf("%d", &b);
join(a, b);
}
} int res = 0;
for(int i = 0; i < n; ++i)
{
if(serch(i) == serch(0))
res++;
}
printf("%d\n", res);
}
return 0;
}
路径压缩可以快,而且只需改一行代码,很爽
//////16 ms 384 KB C++ 1221 B
int serch(int v)
{
if(father[v] == v) return v;
return father[v] = serch(father[v]); ///路径压缩快
}
Poj1611The Suspects的更多相关文章
- poj-1611-The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 34284 Accepted: 16642 De ...
- poj1611---The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 19754 Accepted: 9576 Des ...
- [原]poj-1611-The Suspects(水并查集)
题目链接:http://poj.org/problem?id=1611 题意:输入n个人,m个组.初始化0为疑似病例.输入m个小组,每组中只要有一个疑似病例,整组人都是疑似病例.相同的成员可以在不同的 ...
- [并查集] 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 ...
- DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects
题目传送门 题意:训练指南P250 分析:DFS记忆化搜索,范围或者说是图是已知的字串构成的自动机图,那么用 | (1 << i)表示包含第i个字串,如果长度为len,且st == (1 ...
- poj 1611 The Suspects 并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30522 Accepted: 14836 De ...
- LA 4126 Password Suspects
问题描述:给定m个模式串,计数包含所有模式串且长度为n的字符串的数目. 数据范围:模式串长度不超过10,m <= 10, n <= 25,此外保证答案不超过1015. 分析:既然要计数给定 ...
- POJ 1611 The Suspects (并查集)
The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...
随机推荐
- linux crontab 学习
安装crontab:[root@CentOS ~]# yum install vixie-cron[root@CentOS ~]# yum install crontabs/sbin/service ...
- “无法更新EntitySet“*****”,因为它有一个DefiningQuery,而元素中没有支持当前操作的元素”问题的解决方法
百思不得其解,最后发现 1:实体中的表必须有主键(数据库中的表必须有主键),如果没有,会有这样的提示 2:主键设置好后,运行还是会出现类似问题,那就一个郁闷 1):方法一:先从EF中删除刚设置主键的模 ...
- Mysql事务隔离级别
在说Isolation之前,需要谈谈关系型数据库的ACID特性. A(atomicity,原子性),指一个事务要么完全完成,要么全部回滚到起始状态,不存在中间状态. C(Consistency,一致性 ...
- (转)Ehcache 整合Spring 使用页面、对象缓存
Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...
- 用spring+hibernate+struts 项目记录以及常用的用法进等
一.hibernate1. -----BaseDao------ // 容器注入 private SessionFactory sessionFactory; public void setSessi ...
- codevs 1702素数判定2
Miller-Rabin算法实现,但是一直被判题程序搞,输入9999999999得到的结果分明是正确的但是一直说我错 #include <cstdio> #include <cmat ...
- 二、JavaScript语言--JS基础--JavaScript进阶篇--DOM对象 控制HTML元素
1.认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 先来看看下面 ...
- Android Programming: Pushing the Limits -- Chapter 1: Fine-Tuning Your Development Environment
ADB命令 Application Exerciser Monkey Gradle ProGuard 代码重用 版本控制 静态代码分析 代码重构 开发者模式 ADB命令: @.adb help:查 ...
- C/C++学习笔记----指针的理解
指针是C/C++编程中的重要概念之一,也是最容易产生困惑并导致程序出错的问题之一.利用指针编程可以表示各种数据结构,通过指针可使用主调函数和被调函数之间共享变量或数据结构,便于实现双向数据通讯:指针能 ...
- linux中who命令显示的tty、pts和(:0)(:0.0)是什么意思
http://blog.csdn.net/cwj_beyond/article/details/6987345 http://unix.stackexchange.com/questions/7217 ...