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 ...
随机推荐
- oracle批量插入带主键自增
https://blog.csdn.net/qq_37630354/article/details/82792288
- Spark RDD(Resilient Distributed Dataset)
基于数据集的处理:从物理存储上加载数据,然后操作数据,然后写入物理存储设备.比如Hadoop的MapReduce. 缺点:1.不适合大量的迭代 2. 交互式查询 3. 不能复用曾经的 ...
- 快速搭建angular7 前端开发环境
第一步:全局安装 Angular CLI (1)打开npm(终端)安装angular-cli 第二步:创造工作区和初始应用 (1)运行命令 ng new my-app 第三步:启动开发服务器 (1)c ...
- Git/Bitbucket Workflow
中文 http://blog.jobbole.com/76843/ 英文 https://www.atlassian.com/git/tutorials/comparing-workflows#cen ...
- pytho虚拟环境
pip install virtualenv 进入项目目录 virtualenv venv 激活venv source venv/bin/activate
- windows srver 显示桌面图标。
rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0
- little w and Sum(思维)
链接:https://ac.nowcoder.com/acm/contest/297/B 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
- js 监听浏览器刷新还是关闭事件
转载大神 http://www.cnblogs.com/gavin0517/p/5827405.html
- Murano 网址一览
[Murano_Meeting] http://eavesdrop.openstack.org/#Murano_Meeting [Murano_Meeting_Agenda] https://wiki ...
- java编程如何实现从本地里读取文件1,写入到本地另一个文件2里(多种场景)
不多说,直接上干货! 有时候,我们需要用到这样的一个场景. ReadLocalFile1WriteLocalFile2.java (以下是相当于复制,读取文件1里的全部内容,并写入到文件2里) pac ...