HDU - The Suspects
Description
Input
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
Sample Output
4
1
1 这道题算是我的第一道开始接触并查集类型的题目,我觉得很有纪念意义,虽然搞懂并查集之后比较容易,不过我还是要贴一下代码,毕竟纪念嘛。 一开始我面对这倒题目也是一脸懵比的,因为还不清楚到底怎么写出并查集来,不过在经过菊花哥打通任督二脉之后连下六题倒是很爽,嘻嘻 至于并查集是什么不懂先去百度一下吧,我这里主要还是根据我自己的理解写一点笔记 并查集中用数组来表示集合,本节点储存的是父节点的信息,在查找的时候类似于链表,一路向上查找直到代表一个集合的那个节点,所谓路径压缩, 就是在查找那个集合代表的时候中途也把路过的节点全部指向集合标志节点,要理解这一个递归就好办事了
int FindRoot(int root) {
return road[root] = root ? root : (road[root] = FindRoot(root));
}
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std; const int MX = 33333;
int node[MX];
int num[MX];
int in[MX];
int n, m; void ini() {
for (int i = 0; i < n; i++) {//初始化的时候,因为每一个节点的父节点都是自己本身(规定这样的,嗯,记住就好,以后用多的时候自然就懂了)
num[i] = 1;//这个数组用来记录集合代表节点所包含的元素,当然也包括自己本身,所以初始化为1
node[i] = i;
}
} int FindRoot(int k) {
return node[k] == k ? k : (node[k] = FindRoot(node[k]));
} int main() {
//freopen("input.txt", "r", stdin);
while (scanf("%d %d", &n, &m), n || m) {
ini();
while (m--) {
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++) {
scanf("%d", &in[i]);
if (i == 0) continue;
else {
int root1 = FindRoot(in[i - 1]);
int root2 = FindRoot(in[i]);
if (root1 != root2) {
node[root2] = root1;
num[root1] += num[root2];//合并的时候别忘了把元素个数算上
}
}
}
}
printf("%d\n", num[FindRoot(0)]);//这里要特别注意,因为0的跟节点可能不是0,所以不能直接输num【0】,要更新一遍
}
return 0;
}
HDU - The Suspects的更多相关文章
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- hdu图论题目分类
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- Deci and Centi Seconds parsing in java
http://stackoverflow.com/questions/14558663/deci-and-centi-seconds-parsing-in-java
- Android init.rc执行顺序
转自:http://blog.csdn.net/kickxxx/article/details/7590665 1. 所有的action运行于service之前 2. 下面为各个section的执行 ...
- Hybrid App是如何实现网页语言与程序语言的混合?谁占主体?
[编者按]本文作者@徐珂铭,一位看好Html5的移动互联网的从业人士.喜爱玩技术,会点JAVA.HTML及CSS,有自己的想法及姑且能表达想法的文字,因此有了自己的文章. 基于HTML5的Web Ap ...
- .NET Framework 4 与 .NET Framework 4 Client Profile
今天碰到的一个问题和Client Profile相关的.问题是这样的:一个WPF工程,需要引用另外几个.NET的assembly, 在WPF工程中添加了对这几个assembly的引用,并在程序中可以添 ...
- html5 基本布局+新标签+新选择器 + 线性渐变
html5 基本布局+新标签 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- js this的使用举例
js this的使用举例 <script type="text/javascript"> function test(obj){ obj.style.width= ob ...
- hdu 1517 博弈 **
博弈题: 题意:2 个人玩游戏,从 1 开始,轮流对数进行累乘,直到超过一个指定的值. 解题思路:如果输入是 2 ~ 9 ,因为Stan 是先手,所以Stan 必胜如果输入是 10~18 ,因为Oll ...
- apache https配置步骤
转自:http://www.cnblogs.com/best-jobs/p/3298258.html 1. 确认是否安装ssl模块 是否有mod_ssl.so文件 2. 生成证书和密钥 linux ...
- 安装VS2010水晶报表插件
Visual Studio 2010默认不带水晶报表,需要安装一个水晶报表插件,首先下载此插件: http://downloads.businessobjects.com/akdlm/cr4vs201 ...
- 通讯录(ios自带无界面)
1,添加框架AddressBook.framework 2,请求权限认证,在Appdelegate.m文件中 - (BOOL)application:(UIApplication *)applicat ...