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 ...
随机推荐
- mybatis笔记 - 初始配置及dao的封装
1.用户实体类 package com.javasm.entity; /** * *TODO 用户表实体类 * @author CaoLei 2018年6月26日上午10:50:12 * Manage ...
- cf873F(xjb+二分)
题目链接:http://codeforces.com/problemset/problem/837/F 题意:给出一个大小为 n 的数组 a 和一个数 k,每次操作后的到一个 a' 数组,a'i 为 ...
- CF 980D Perfect Groups(数论)
CF 980D Perfect Groups(数论) 一个数组a的子序列划分仅当这样是合法的:每个划分中的任意两个数乘积是完全平方数.定义a的权值为a的最小子序列划分个数.现在给出一个数组b,问权值为 ...
- 洛谷P3068 [USACO13JAN]派对邀请函Party Invitations
P3068 [USACO13JAN]派对邀请函Party Invitations 题目描述 Farmer John is throwing a party and wants to invite so ...
- ios一个自定义的下拉多选菜单
前段时间项目刚好要做到条件筛选菜单,正好找到一些别人写的,结合自己实际需求进行优化修改,一个实用的多条件筛选菜单,根据其他的下拉进行一些改进. 点击后返回点击文字显示 github地址:https:/ ...
- Linux表面工作
https://www.cnblogs.com/welhzh/p/5899875.html 1.显示文本文件的内容 /etc/issue #登录前提示 /etc/motd #登录后提示 ssh ...
- 使用JMeter进行API功能测试
使用JMeter进行API功能测试 Apache JMeter是一种流行的开源软件,用于性能测试. 在本博客中,我们将阐明如何使用JMeter for REST API自动化进行功能测试. 我们使用了 ...
- eclipse对于虚拟内存的溢出处理
第一个配置:-Xms1024m -Xmx2048m 第二个配置: 第二个配置:-XX:MaxPermSize=1024m 第三个配置就是eclipse安装包中的eclipse.ini文件 -Xms51 ...
- haoi2018奇怪的背包题解
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=5302 对于一个物品,设它体积为v,那么,在背包参数为p的情况下,它能达到gcd(v,p ...
- web.xml中如何设置配置文件的加载路径
web应用程序通过Tomcat等容器启动时,会首先加载web.xml文件,通常我们工程中的各种配置文件,如日志.数据库.spring的文件等都在此时被加载,下面是两种常用的配置文件加载路径,即配置文件 ...