hdu-1856 More is better---带权并查集
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1856
题目大意:
一个并查集 计算每个集合的元素 找出元素最多的那个集合,输出元素的个数
解题思路:
输入n=0时也应该输出1
可以用set储存每个元素,Map更新每个元素的根节点的权值
还可以用带权并查集,在合并集合的时候,合并带权的数组(该数组表示集合中的元素)
set&map版:
#include<bits/stdc++.h>
using namespace std;
int T, n, m;
const int maxn = + ;
int cases;
int p[maxn];
void init()
{
for(int i = ; i < maxn; i++)p[i] = i;
}
int Find(int x)
{
return x == p[x] ? x : p[x] = Find(p[x]);
}
set<int>s;
map<int, int>Map;
int main()
{
int n, x, y;
while(scanf("%d", &n) != EOF)
{
s.clear();
Map.clear();
init();
while(n--)
{
scanf("%d%d", &x, &y);
{
p[Find(x)] = Find(y);
s.insert(x);
s.insert(y);
}
}
int ans = , t;
for(set<int>::iterator it = s.begin(); it != s.end(); it++)
{
t = Find(*it);
ans = max(ans, ++Map[t]);
}
printf("%d\n", ans);
}
return ;
}
带权并查集版:
#include<bits/stdc++.h>
using namespace std;
int T, n, m;
const int maxn = + ;
int cases;
int p[maxn], cnt[maxn];//cnt数组计数
void init()
{
for(int i = ; i < maxn; i++)p[i] = i, cnt[i] = ;
}
int Find(int x)
{
return x == p[x] ? x : p[x] = Find(p[x]);
}
int main()
{
int n, x, y;
while(scanf("%d", &n) != EOF)
{
init();
while(n--)
{
scanf("%d%d", &x, &y);
{
x = Find(x);
y = Find(y);
if(x != y)p[x]= y, cnt[y] += cnt[x];//x那颗树作为y的那棵树的子树 }
}
int ans = cnt[];
for(int i = ; i < maxn; i++)
ans = max(ans, cnt[i]);
printf("%d\n", ans);
}
return ;
}
hdu-1856 More is better---带权并查集的更多相关文章
- HDU(1856),裸的带权并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1856 题意:朋友圈问题,A和B是朋友,B和C是朋友则A和C也是朋友,依次类推,题目的意思就是求最大的朋 ...
- HDU 3047 Zjnu Stadium(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...
- hdu 3074 Zjnu Stadium (带权并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3635 Dragon Balls(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意: 有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城 ...
- HDU 3047 Zjnu Stadium(带权并查集,难想到)
M - Zjnu Stadium Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]
传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- 【带权并查集】HDU 3047 Zjnu Stadium
http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...
- HDU 3047 带权并查集 入门题
Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...
随机推荐
- MATLAB求解非线性方程组
matlab中有专门的solve函数来解决方程组的(a-x)^2+(b-y)^2=e^2(C-x)^2+(D-y)^2=v^2已知a,b,c,d,e,v 值求解 X,Y 请问用 matlab 如何写, ...
- yarn快速使用及实践建议
什么是 yarn? 简单来说,yarn 是一个与 npm 功能相同的工具,用于前端项目的依赖管理.在使用 npm 的项目中,使用 npm 命令的地方都可以使用 yran 来代替. 为什么要使用 yar ...
- 洛谷P2202 [USACO13JAN]方块重叠Square Overlap
P2202 [USACO13JAN]方块重叠Square Overlap 题目描述 Farmer John is planning to build N (2 <= N <= 50,000 ...
- Hadoop集群配置免密SSH登录方法
Hadoop集群包含1个主节点和3个从节点,需要实现各节点之间的免密码登录,下面介绍具体的实现方法. 一.Hadoop集群环境 二.免密登录原理 每台主机authorized_keys文件里面包含的主 ...
- 剑指Offer的学习笔记(C#篇)-- 旋转数组的最小数字
题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋 ...
- LeetCode 136 Single Number 数组中除一个数外其他数都出现两次,找出只出现一次的数
Given an array of integers, every element appears twice except for one. Find that single one. class ...
- POJ 1061青蛙的约会。求解(x+mT)%L=(y+nT)%L的最小步数T。
因为是同余,所以就是(x+mT)%L-(y+nT)%L=0.可以写成(x-y+(m-n)T)%L=0.就是这个数是L的倍数啦.那么我可以这样x-y+(m-n)T + Ls = 0.就可以了,s可正可负 ...
- 【Windows】命令行查询占用端口信息
Windows上查询443端口被占用的信息: NETSTAT.EXE -nao | findstr "443" 查询结果 杀掉进程: tskill 2888
- HDU 5445——Food Problem——————【多重背包】
Food Problem Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...
- Docker | 第一章:Docker简介
前言 作为本系列的起始章节,本章节主要是对Docker的相关概念进行简单阐述下.自此也是查阅了相关资料,奈何也都是英文版居多,看的是有点头大的.现在悔不当初不好好学习英文了.o(︶︿︶)o 唉 Doc ...