C - The Suspects POJ - 1611(并查集)
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相连的节点的个数
题解:通过并查集将所有的结点连接在一起,在连接两个不同的组时,将边数少的连接到变数少的连接的变数多的的上面
1 #include<iostream>
2
3 using namespace std;
4
5 int pre[30005];
6 int Rank[30005];
7
8 int Find(int x) {
9 return pre[x] = pre[x] == x ? x : Find(pre[x]);
10 }
11
12 void Union(int x, int y) {
13 int t1, t2;
14 t1 = Find(x);
15 t2 = Find(y);
16 if (t1 == t2)
17 return ;
18 if (Rank[t1] > Rank[t2]) {
19 pre[t2] = t1;
20 }
21 else {
22 pre[t1] = t2;
23 if (Rank[t1] == Rank[t2]) {
24 pre[t1] = t2;
25 Rank[t2]++;
26 }
27 }
28 }
29
30 int main() {
31 int n, m;
32 while(cin >> n >> m) {
33
34 if (n == 0 && m == 0)
35 break;
36 // 初始化父亲节点
37 for (int i = 0; i <= n; i++) {
38 pre[i] = i;
39 Rank[i] = 0;
40 }
41
42 for (int i = 1; i <= m; i ++) {
43 int t, k, h, r, flag;
44 cin >> t;
45 if (t > 0)
46 cin >> r;
47
48 for (int j = 1; j < t; j++) {
49 cin >> k;
50 Union(r, k);
51 }
52
53 }
54 int ans = 1, cmp = Find(0);
55
56 for (int i = 1; i < n; i++) {
57 if (cmp == Find(i))
58 ans++;
59 }
60 cout << ans << endl;
61 }
62
63 return 0;
64 }
C - The Suspects POJ - 1611(并查集)的更多相关文章
- The Suspects(POJ 1611 并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30158 Accepted: 14665 De ...
- poj 1611(并查集)
http://poj.org/problem?id=1611 题意:有个学生感染病毒了,只要是和这个学生接触过的人都会感染,而和这些被感染者接触的人,也会被感染,现在给定你一些协会的人数,以及所在学生 ...
- POJ 1611(并查集+知识)
并查集主要是两个过程,一个是并,一个是查 原理是用一个数组p[i]保存每个i的根节点,如果根节点一样则在同一个集合里,所以只有根节点p[i]=i; 查: int find(int x){return ...
- POJ 1611并查集
我发现以后写题要更细心,专心! #include<iostream>#include<algorithm>#include<stdio.h>#include< ...
- poj 1984 并查集
题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...
- poj 1611 :The Suspects经典的并查集题目
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
- 【POJ】The Suspects(裸并查集)
并查集的模板题,为了避免麻烦,合并的时候根节点大的合并到小的结点. #include<cstdio> #include<algorithm> using namespace s ...
- poj 1797(并查集)
http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...
- POJ 2492 并查集扩展(判断同性恋问题)
G - A Bug's Life Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- 学习java之电脑的常用快捷键和DOS窗口下的常用命令
学习java之电脑的常用快捷键和DOS窗口下的常用命令 电脑一些常用的快捷键 win快捷键: 单独按Windows:显示或隐藏 "开始"功能表 Windows+BREAK:显示&q ...
- sentry SSRF
目录 Sentry介绍 exp测试步骤 自己构造blind发包 修复方式 参考 Sentry介绍 Sentry 是一个实时的事件日志和聚合平台,基于 Django 构建.一般在url上.或者logo上 ...
- 翻译:《实用的Python编程》03_04_Modules
目录 | 上一节 (3.3 错误检查) | 下一节 (3.5 主模块) 3.4 模块 本节介绍模块的概念以及如何使用跨多个文件的函数. 模块和导入 任何一个 Python 源文件都是一个模块. # f ...
- [源码分析] 消息队列 Kombu 之 Consumer
[源码分析] 消息队列 Kombu 之 Consumer 目录 [源码分析] 消息队列 Kombu 之 Consumer 0x00 摘要 0x01 综述功能 0x02 示例代码 0x03 定义 3.1 ...
- 基于CefSharp开发浏览器(九)浏览器历史记录弹窗面板
一.前言 前两篇文章写的是关于浏览器收藏夹的内容,因为收藏夹的内容不会太多,故采用json格式的文本文件作为收藏夹的存储方式. 关于浏览器历史记录,我个人每天大概会打开百来次网页甚至更多,时间越长历史 ...
- WPF 基础 - xaml 语法总结
Attribute 与 Property 之间的区别 Property 对应着抽象对象身上的性状: Attribute 是针对标签的特征: 往往一个标签具有的 Attribute 对于它所代表的对象的 ...
- 微软跨平台UI框架MAUI真的要来啦
.NET 6 preview已经上线,是时候为在BUILD 2020上宣布的新.NET Multi-platform App UI(MAUI)做准备了.对于客户端应用程序开发人员来说,这一年.NET有 ...
- 如何让python脚本支持命令行参数--getopt和click模块
一.如何让python脚本支持命令行参数 1.使用click模块 如何使用这个模块,在我前面的博客已经写过了,可参考:https://www.cnblogs.com/Zzbj/p/11309130.h ...
- switch case语句,switch case用法详解
switch 是"开关"的意思,它也是一种"选择"语句,但它的用法非常简单.switch 是多分支选择语句.说得通俗点,多分支就是多个 if. 从功能上说,sw ...
- Linux+mysql混杂
一.linux 1.linux中给某一文件中批量新增一个内容 先vim进入文件,然后先按ctrl+v 然后选中需要的行数, 在shift+i 写你要添加的东西 然后在按下esc 二,mysql 1.l ...