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 ...
随机推荐
- Mysql Condition /Handler(异常处理)
关于介绍,可参见:http://www.cnblogs.com/end/archive/2011/04/01/2001946.html. http://blog.csdn.net/rdarda/art ...
- hdu 1513
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 思路:正反分别求一次LCS,利用滚动数组对二取余滚动 #include<stdio.h&g ...
- 第二课 less的学习以及移动端需要注意的问题
一.LESS的学习笔记: 1.less介绍:一种动态样式语言.less将css赋予了动态语言的特性,如变量,继承,运算,函数,less既可以在客户端上运行(支持IE6+,webkit,firefox) ...
- Memcached驱动(C#)
using Memcached.ClientLibrary; using System; using System.Collections.Generic; using System.IO; usin ...
- Delphi之DLL知识学习1---什么是DLL
DLL(动态链接库)是程序模块,它包括代码.数据或资源,能够被其他的Windows 应用程序共享.DLL的主要特点之一是应用程序可以在运行时调入代码执行,而不是在编译时链接代码,因此,多个应用程序可以 ...
- 华为Mate8 NFC 时好时坏,怎么解决呢?
拿起手机朝桌子上磕几下,nfc就好用了.这是花粉总结的,我也试过,很灵.注意要带套,摄像头朝下,头部低一点往下磕.因为nfc芯片在头部,估计是接触不良.
- Oralce sysaux WRH$_ACTIVE_SESSION_HISTORY清理
In this Document Symptoms Cause Solution References Symptoms sysaux表空間的WRH$_ACTIVE_SESSION_HISTORY表變 ...
- 基于socket、多线程的客户端服务器端聊天程序
服务器端: using System; using System.Windows.Forms; using System.Net.Sockets; using System.Net;//IPAddre ...
- 一个json字符串
{ "area": [{ "flag": "Y", "ishot": "N", "lag& ...
- sql修改约束语法练习
--以系统管理员身份登录到SQL Server服务器,并使用T-SQL语句实现以下操作:--1. 将stu数据库中student表的sno定义为主键:alter table [student] add ...