HDU ACM 1856 More is better(并查集)
【题目链接】http://acm.hdu.edu.cn/showproblem.php?pid=1856
【解题思路】给的数据有点大,干脆少开点数组,直接上set存储有朋友的孩子的编号,同时根据编号初始化对应的在father数组中的下标,同时也更新集合,后面开一个数组存储同属于于一个集合的数有多少个(在以根为数组的下标中存储),然后遍历一次找到最大的数
- #include <cstdio>
- #include <cstring>
- #include <set>
- #include <algorithm>
- #define SIZE 10000001
- #define MAXN 100002
- using namespace std;
- int father[SIZE];
- int num[SIZE];
- set<int>exist;
- int find(int f)
- {
- return father[f] = f == father[f] ? f : find(father[f]);
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- #endif
- int n;
- while(scanf("%d", &n) != EOF)
- {
- if(!n)
- {
- printf("1\n");
- continue;
- }
- exist.clear();
- memset(num, , sizeof(num));
- for(int i=; i<n; ++i)
- {
- int u, v;
- scanf("%d%d", &u, &v);
- if(exist.find(u) == exist.end())
- {
- father[u] = u;
- exist.insert(u);
- num[u] = ;
- }
- if(exist.find(v) == exist.end())
- {
- exist.insert(v);
- father[v] = v;
- num[v] = ;
- }
- u = find(u);
- v = find(v);
- father[u] = v;
- }
- for(set<int>::const_iterator iter = exist.begin(); iter != exist.end(); ++iter)
- {
- num[find(*iter)]++;
- }
- int curmax = ;
- for(set<int>::const_iterator iter = exist.begin(); iter != exist.end(); ++iter)
- {
- if(curmax < num[*iter]) curmax = num[*iter];
- }
- printf("%d\n", curmax);
- }
- return ;
- }
HDU ACM 1856 More is better(并查集)的更多相关文章
- hdu 5458 Stability(树链剖分+并查集)
Stability Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total ...
- [HDU 3712] Fiolki (带边权并查集+启发式合并)
[HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...
- HDU 1856 More is better (并查集)
题意: 给你两个数代表这两个人是朋友,朋友的朋友还是朋友~~,问这些人组成的集合里面人最多的是多少... 思路: 属于并查集了,我用的是带路径压缩的,一个集合里面所有元素(除了根节点)的父节点都是根节 ...
- ACM学习历程—HDU 1272 小希的迷宫(并查集)
Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
- hdu 5652 India and China Origins 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- HDU 5458 Stability (树链剖分+并查集+set)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 给你n个点,m条边,q个操作,操作1是删边,操作2是问u到v之间的割边有多少条. 这题要倒着做才 ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- hdu 5652 India and China Origins 并查集+逆序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...
- HDU 5809 Ants(KD树+并查集)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5809 [题目大意] 给出一些蚂蚁和他们的巢穴,一开始他们会在自己的巢穴(以二维坐标形式给出),之后 ...
随机推荐
- CSS 将一个页面平均分成四个部分(div)
在项目中遇到需求,数据监控页面需要同时显示4个板块内容,如下图: CSS 如何将一个页面平均分成四个部分(div)呢? <!DOCTYPE html> <html lang=&quo ...
- 多线程读者写者问题--用QT实现
先把代码贴上来,有时间再整理吧..因为工作中用Qt,所以用Qt实现的....刚上班,,忙! 三种方法,读者优先,写者优先和公平竞争.. 读者优先 #include <QCoreApplicati ...
- Idea 编写 Spark 示例代码并打包成Jar
说明:本人是在Linux下搭建的单机Spark环境,也是在Linux下使用Idea14.02进行代码编辑 1. 打开IDEA,在欢迎界面从右下角的Configure -> Plugins进入,安 ...
- Spring @RequestAttribute
@RequestAttribute注解用法 @RequestAttribute用在方法入参上,作用:从request中取对应的值,至于request中是怎么存在该属性的,方式多种多样,拦截器中预存.M ...
- 自动化运维(1)之二进制部署MySQL5.7
二进制部署MySQL5.7 这个文档用于基础解释,后面通过ansible的自动化对MySQL单实例进行安装部署. 1.解压文件 # tar zxvf mysql-5.7.22-linux-glibc2 ...
- [转]在Node.js中使用RabbitMQ系列一 Hello world
本文转自:https://www.cnblogs.com/cpselvis/p/6288330.html 在前一篇文章中可伸缩架构简短系列中提到过关于异步的问题.当时推荐使用RabbitMQ来做任务队 ...
- csharp: Setting the value of properties reflection
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 二维码编码与解码类库ThoughtWorks.QRCode
官方地址:https://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library 有源代码和示例程序 支持二维码编码(生成)和解码( ...
- Windows安装diango框架<一>
下一篇:使用Django创建网站项目<二> python工具安装 python下载:https://www.python.org/downloads/windows/(我的版本3.7.0) ...
- MyBatis学习笔记(三) Configuration类
一.初探Configuration类 我们先来看一下MyBatis的XML配置文件的结构,(摘自mybatis.org) 下面这个是Configuration类的部分变量 一点不一样是不是??? 其实 ...