POJ 1611 The Suspects 并查集 Union Find
本题也是个标准的并查集题解。
操作完并查集之后,就是要找和0节点在同一个集合的元素有多少。
注意这个操作,须要先找到0的父母节点。然后查找有多少个节点的额父母节点和0的父母节点同样。
这个时候须要对每一个节点使用find parent操作。由于最后状态的时候,节点的parent不一定是本集合的根节点。
#include <stdio.h> const int MAX_N = 30001;
struct SubSet
{
int p, rank;
}sub[MAX_N]; int N, M; void initSub()
{
for (int i = 0; i < N; i++)
{
sub[i].p = i;
sub[i].rank = 0;
}
} int find(int x)
{
if (x != sub[x].p) sub[x].p = find(sub[x].p);
return sub[x].p;
} void unionTwo(int x, int y)
{
int xroot = find(x);
int yroot = find(y);
if (sub[xroot].rank < sub[yroot].rank) sub[xroot].p = yroot;
else
{
if (sub[xroot].rank == sub[yroot].rank) sub[xroot].rank++;
sub[yroot].p = xroot;
}
} int main()
{
int a, b, k;
while (scanf("%d %d", &N, &M) && (N || M))
{
initSub();
for (int i = 0; i < M; i++)
{
scanf("%d", &k);
if (k > 0) scanf("%d", &a);
for (int j = 1; j < k; j++)
{
scanf("%d", &b);
unionTwo(a, b);
}
}
int sus = 1, p = find(0);
for (int i = 1; i < N; i++)
{
if (find(i) == p) sus++;
}
printf("%d\n", sus);
}
return 0;
}
POJ 1611 The Suspects 并查集 Union Find的更多相关文章
- poj 1611 The Suspects(并查集输出集合个数)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj 1611 The Suspects 并查集变形题目
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 20596 Accepted: 9998 D ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
- POJ 1611 The Suspects (并查集求数量)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj 1611 The Suspects 并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30522 Accepted: 14836 De ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 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 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
随机推荐
- 2017年6月28日 python爬虫学习
1.写入csv文件2.lxml的用法3.自定义字典类的方法4.bytes解码得到str,str编码得到bytes5.json 1 import csv import lxml.html class S ...
- BZOJ2870 最长道路tree(并查集+LCA)
题意 (n<=50000) 题解 #include<iostream> #include<cstring> #include<cstdio> #include ...
- [vue插件]基于vue2.x的电商图片放大镜插件
最近在撸一个电商网站,有一个需求是要像淘宝商品详情页那样,鼠标放在主图上,显示图片放大镜效果,找了一下貌似没有什么合适的vue插件,于是自己撸了一个,分享一下.小白第一次分享,各位大神莫见笑. vue ...
- C#之改变窗体icon图标、新建类文件、调用dll库
一.改变窗体的图标 没有修改之前为: 修改之后为自己想要的图标: 需要在窗体Form1.cs属性里边添加icon图片文件: 二.新建cs类文件 如下图所示,新建一个类文件,我用于来调用库文件也可以来定 ...
- [Transducer] Lazyness in Transduer
Transducers remove the requirement of being lazy to optimize for things like take(10). However, it c ...
- POJ 2442 Sequence(堆的使用练习)
题目地址:id=2442">POJ 2442 真心没想到这题的思路. .原来是从第一行逐步向下加,每次都仅仅保存前n小的数.顺便练习了下堆.. 只是感觉堆的这样的使用方法用的不太多啊. ...
- HDOJ1084 What Is Your Grade?
What Is Your Grade? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Android-Universal-Image-Loader 的使用说明
这个图片异步载入并缓存的类已经被非常多开发人员所使用,是最经常使用的几个开源库之中的一个,主流的应用,随便反编译几个火的项目,都能够见到它的身影. 但是有的人并不知道怎样去使用这库怎样进行配置,网上查 ...
- HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- 【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】
[129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...