题目链接:

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. MySql中存储过程的理解

    到底什么是存储过程,又为什么需要使用存储过程? 存储过程简单来说,就是为以后的使用而保存的一条或多条MySQL语句的集合,可将其视为批文件,虽然它们的作用不仅限与批处理. 使用存储过程有3个主要的好处 ...

  2. 如何使用ros命令行显示图片

    rosrun image_view image_view image:=[TOPIC] 注意:每次只能显示一个UI.不能在一条命令中订阅多个节点.

  3. oracle语句解析顺序

  4. Spark SQL 读到的记录数与 hive 读到的不一致

    问题:我用 sqoop 把 Mysql 中的数据导入到 hive,使用了--delete-target-dir --hive-import --hive-overwrite 等参数,执行了两次. my ...

  5. Java Script 第二章.

    对象: JavaScript中的所有事物都是对象:字符串,数组,数值,函数..... JavaScript中提供多个内建对象,比如说 String,  Date,  Array等等.对象只是带有属性和 ...

  6. CF446B DZY Loves Modification 优先队列

    As we know, DZY loves playing games. One day DZY decided to play with a n × m matrix. To be more pre ...

  7. 消息中间件的研究(二) RabbitMQ应用场景分析

    分析一下六个场景下RabbitMQ的应用: 1.爬虫 2.智能家居云平台 3.电子商务系统 4.实时监控系统 5.海量日志的分布式处理 6. 智能交通管控平台中数据分析子系统     1.爬虫     ...

  8. Testing Round #12 B

    Description A restaurant received n orders for the rental. Each rental order reserve the restaurant ...

  9. 为什么要问Servlet的初始化时间

    Servlet的init方法到底是在什么时候调用的? j2ee specification和java doc中有以下说明 如果load-on-startup设置为>=0, 部署的时候就会调用.  ...

  10. Linux Shell 中数组的语法及应用

    #!/bin/sh## 数组的声明与初始化方法# 先声明后赋值:declare -a arrayarray=(one two three) # 声明并初始化:array_1=(1 2 3 four) ...