题目链接:

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---带权并查集的更多相关文章

  1. HDU(1856),裸的带权并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1856 题意:朋友圈问题,A和B是朋友,B和C是朋友则A和C也是朋友,依次类推,题目的意思就是求最大的朋 ...

  2. HDU 3047 Zjnu Stadium(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...

  3. hdu 3074 Zjnu Stadium (带权并查集)

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. HDU 3635 Dragon Balls(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意: 有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城 ...

  5. HDU 3047 Zjnu Stadium(带权并查集,难想到)

    M - Zjnu Stadium Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  6. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  7. 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 ...

  8. 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 ...

  9. 【带权并查集】HDU 3047 Zjnu Stadium

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...

  10. HDU 3047 带权并查集 入门题

    Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...

随机推荐

  1. python 之 函数 基础2

    5.36 命名关键字 什么是命名关键字参数? 格式:在*后面参数都是命名关键字参数 特点: 1 必须被传值 2 约束函数的调用者必须按照key=value的形式传值 3 约束函数的调用者必须用我们指定 ...

  2. 帝都Day5——依旧是数据结构

    /*Day1.Day2我尽量整理吧*/ 树状数组 树状数组滋瓷单点修改和前缀查询 加特技可以使得树状数组支持更多操作. c[2n+1]=a[2n+1](奇数就是它本身) c[2n]≠a[2n](偶数不 ...

  3. Java 实现大转盘抽奖

    需要用到 JAVA中的Random()函数 注意:大转盘抽奖各奖项中奖概率之和为 1.奖品列表中的概率为累加概率,需要按照添加进列表的顺序进行累加,添加顺序不做要求. 实际中使用需要考虑奖品数量限制等 ...

  4. 关系型数据库基础概念:MySQL系列之开篇

    一.基础概念 数据(Data)是描述事物的符号记录,是指利用物理符号记录下来的.可以鉴别的信息. 1.数据库(Database,DB)是指长期储存在计算机中的有组织的.可共享的数据集合.数据要按照一定 ...

  5. 解决tomcat闪退问题

    https://blog.csdn.net/zh2nd/article/details/79068680 转载此博客链接内容,非常感谢博主 本文参考CSDN博主 哈克沃德.的<Tomcat8启动 ...

  6. vector的学习

    学习链接:http://www.runoob.com/cplusplus/cpp-stl-tutorial.html kandaima #include<iostream> #includ ...

  7. Tree and Queries CodeForces - 375D 树上莫队

    http://codeforces.com/problemset/problem/375/D 树莫队就是把树用dfs序变成线性的数组. (原数组要根据dfs的顺序来变化) 然后和莫队一样的区间询问. ...

  8. Seven-segment Display 贪心选择,快速判断能否有解

    https://csacademy.com/contest/round-39/task/seven-segment-display/ 可以知道,只有1是无解 而且肯定是选出来的位数约小越好. 位数 = ...

  9. Spark Mllib里的卡方检验

    不多说,直接上干货! import org.apache.spark.mllib.stat.Statistics 具体,见 Spark Mllib机器学习实战的第4章 Mllib基本数据类型和Mlli ...

  10. eclipse链接Hadoop集群时报错Error:Call From xxx/xxx.xxx.xxx.xxx to hostname1:9000 failed on connection exception

    今天用eclipse连接Hadoop集群的时候突然给我报了这样一个错误:Error:Call From xxx/xxx.xxx.xxx.xxx to hostname1:9000 failed on ...