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 ...
随机推荐
- @ifconfig eth0|awk -F "[ :]+" 'NR==2{print $4 "/" $NF}'中"[ :]+" 是什么意思?@
http://blog.csdn.net/zhuying_linux/article/details/6822987
- C# 根据ADO.NET数据库连接字符串构建EntityFrame数据库连接字符串
为了保持开发效率,以及保持代码优雅,项目中引用了EntityFrame.但是又因为某些报表功能需要大量计算,所以又要求直接使用ADO.NET,调用存储过程进行计算. 于是乎webconfig文件中就会 ...
- 在ubuntu上搭建开发环境1---在windows7的基础上在安装ubuntu(双系统)
转载:http://jingyan.baidu.com/article/60ccbceb18624464cab197ea.html 当需要频繁使用ubuntu时,vmware虚拟机下运行ubuntu, ...
- qsort函数详解
C语言标准库函数 qsort 详解 文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作. 原文链接:http://www.slyar.c ...
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件. 1.相关文 ...
- oracle限制ip訪問
我這oracle版本為11.2.0.4,裝的GRID,所以在grid用戶下編輯sqlnet.ora 1.cd /grid/product/11.2.0/network/admin 2.編輯sqlne ...
- android selector(转)
Selector的结构描述: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:a ...
- Asp.Net MVC中Controller与View之间传递的Model
Controller --> View 的Model 与 提交表单后 View --> Controller 的Model 不是相同的对象,即:这两个Model为不同的指针,指向不同的地址 ...
- Released Mocked Streams for Apache Kafka
Kafka Streams is a deployment-agnostic stream processing library written in Java. Even thoug ...
- Liferay 6.2 改造系列之十二:修改Portal设置页面表单内容
将Portal设置页面中无用的内容删除: 在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Input a list of ...