[原]poj-1611-The Suspects(水并查集)
题目链接:http://poj.org/problem?id=1611
题意:输入n个人,m个组。初始化0为疑似病例。输入m个小组,每组中只要有一个疑似病例,整组人都是疑似病例。相同的成员可以在不同的组。找出一共有多少个疑似病例。
解题思路:同组的同parent,查找,合并集合。最后将出现的每个组员的parent和0的parent相比较,统计便可。
AC代码:
- #include<iostream>
- #include<algorithm>
- using namespace std;
- #define ma 300010
- int fa[ma],m,n;
- int h[ma];
- int member[ma];
- int Count = 1;
- void init(int a)
- {
- for(int i = 0; i < a; i++)
- {
- fa[i] = i;
- h[i] = 0;
- }
- return;
- }
- int Find(int x)
- {
- if(fa[x] == x)
- return x;
- else
- return fa[x] = Find(fa[x]);
- }
- void unite(int x,int y)
- {
- x = Find(fa[x]);
- y = Find(fa[y]);
- if(x == y) return;
- else
- {
- if(h[x] > h[y]) fa[y] = fa[x];
- else
- {
- fa[x] = fa[y];
- if(h[x] == h[y]) h[y]++;
- }
- }
- }
- bool same(int x, int y)
- {
- return Find(x) == Find(y);
- }
- inline void solve(int n,int m)
- {
- init(n);
- while(m--)
- {
- int a;
- cin>>a;
- for(int i = 0; i < a; i++)
- cin>>member[i];
- //sort(member,member+a*sizeof(int));
- for(int i = 1; i < a; i++)
- unite(member[i-1],member[i]);
- }
- for(int i = 1; i < n; i++)
- if(same(0,i)) Count++;
- cout<<Count<<endl;
- }
- int main()
- {
- while(cin>>n>>m)
- {
- if(!n) break;
- solve(n,m);
- Count = 1;
- }
- return 0;
- }
[原]poj-1611-The Suspects(水并查集)的更多相关文章
- poj 1611:The Suspects(并查集,经典题)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21472 Accepted: 10393 De ...
- POJ 1611 The Suspects (并查集)
The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...
- poj 1611 The Suspects(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 De ...
- POJ 1611 The Suspects(并查集,简单)
为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...
- poj 1611 The Suspects(简单并查集)
题目:http://poj.org/problem?id=1611 0号是病原,求多少人有可能感染 #include<stdio.h> #include<string.h> # ...
- POJ - 1611 The Suspects 【并查集】
题目链接 http://poj.org/problem?id=1611 题意 给出 n, m 有n个人 编号为 0 - n - 1 有m组人 他们之间是有关系的 编号为 0 的人是 有嫌疑的 然后和 ...
- POJ 1611 The Suspects【并查集】
解题思路:一共给出 n个人,m组,接下来是m组数据,每一组开头是该组共有的人 num,则接下来输入的num个数,这些数是一组的 又因为最开始只有编号为0的人携带有病毒,且只有同一组的人会相互传染,问最 ...
- 并查集 (poj 1611 The Suspects)
原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
随机推荐
- 【POJ】【1821】Fence
DP/单调队列优化 题意:k个人粉刷总长为n的墙壁(或者说栅栏?),每个人有一个必刷点s[i](这个人也可以一点也不刷,如果刷就必须刷这个点),最大粉刷长度l[i](必须是连续粉刷一段),和粉刷一格的 ...
- windows phone中ListBox的简单使用
学习windows phone数据绑定的一点点心得,在wp系统的APP中经常遇到这样风格的软件,那它们到底怎样实现的呢?我就大致去做了一下,比较粗虐,但基本的都已经有了,实现后的结果为: 哇,这个图截 ...
- linux下解压命令大全(转载)
.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压 ...
- 移动端页面调试工具——UC浏览器开发者版
在移动页面的开发中,我们很难像PC端那样很方便的调试,网上也有各种各样的调试方式.但在工作中,我主要还是用chorme自带的模拟器来模拟各种移动设备,但是用久了之后发现毕竟是模拟的,与真机调试还是会有 ...
- mysql存储过程和函数使用实例
1.需求:根据输入的年份,月份,和当前系统的年份比较,不满1年按1年计算,多出1年11个月也按1年计算. 2.计算得出来的使用年份,计算车辆残值. 3.存储过程 DELIMITER $$ USE `d ...
- CSS 加载新方式
Chrome 浏览器有意改变<link rel="stylesheet">的加载方式,当其出现在<body>中时,这一变化将更加明显.笔者决定在本文中进行详 ...
- Javascript Date类常用方法详解
getDate :得到的是今天是 几号(1-28.29.30.31). getDay : 得到的是今天是 星期几(1-7). getFullYear : 得到的是今天是 几几年(4位). getH ...
- CodeIgniter API
http://apigen.juzna.cz/doc/EllisLab/CodeIgniter/tree.html Classes CI_Benchmark CI_Calendar CI_Cart C ...
- javascript console
javascript console console.log(object[, object, ...])在控制台输出一条消息.如果有多个参数,输出时会用空格隔开这些参数. 第一个参数可以是一个包含格 ...
- ExtJs布局之table
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...