求集合里元素的个数 输出最大的个数是多少

Sample Input
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8

Sample Output
4
2

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# include <queue>
# define LL long long
using namespace std ; const int MAXN = ;
int F[MAXN];
int num[MAXN] ; int find(int x)//找x的祖先结点
{
if(F[x]==x) return x;
return F[x]=find(F[x]);
}
void bing(int u,int v) //按秩合并
{
int x = find(u);
int y = find(v);
if(x == y)
return ;
if(num[x] >= num[y])
{
F[y] = x;
num[x] += num[y];
}
else
{
F[x] = y;
num[y] += num[x];
}
}
int main()
{
//freopen("in.txt","r",stdin) ;
int n ;
while(scanf("%d",&n) != EOF)
{
if (n == )
{
printf("1\n") ;
continue ;
}
int i ;
for(i = ; i <= MAXN ; i++)
{
F[i] = i ;
num[i] = ;
}
int u , v , t = ;
for(i = ; i <= n ; i++)
{
scanf("%d %d" , &u , &v) ;
bing(u,v) ;
t = max(t , max(u , v)) ;
}
int Max = ;
for(i = ; i <= t ; i++)
if (num[i] > Max)
Max = num[i] ;
printf("%d\n" , Max) ; }
return ;
}

hdu 1856 求集合里元素的个数 输出最大的个数是多少的更多相关文章

  1. 定义一个Collection接口类型的变量,引用一个Set集合的实现类,实现添加单个元素, 添加另一个集合,删除元素,判断集合中是否包含一个元素, 判断是否为空,清除集合, 返回集合里元素的个数等常用操作。

    package com.lanxi.demo2; import java.util.HashSet; import java.util.Iterator; import java.util.Set; ...

  2. 从两个List集合里找到相同部分和不同部分

    /** * 获取两个集合里元素不同的部分 */ public List<User> getDifferent(List<User> u1, List<User> u ...

  3. poj 1611 求0号结点所在集合的元素个数

    求0号结点所在集合的元素个数 Sample Input 100 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0Sample Out ...

  4. 求集合中选一个数与当前值进行位运算的max

    求集合中选一个数与当前值进行位运算的max 这是一个听来的神仙东西. 先确定一下值域把,大概\(2^{16}\),再大点也可以,但是这里就只是写写,所以无所谓啦. 我们先看看如果暴力求怎么做,位运算需 ...

  5. 有一个集合,判断集合里有没有“world”这个元素,如果有,添加“javaee”

    // 有一个集合,判断集合里有没有“world”这个元素,如果有,添加“javaee” List list = new ArrayList(); list.add("world") ...

  6. 删除列表中重复元素以及求list中元素个数

    Python 去除列表中重复的元素 来自比较容易记忆的是用内置的set l1 = ['b','c','d','b','c','a','a'] l2 = list(set(l1)) print l2 还 ...

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

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

  8. HDU 1856

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1856 对于这道题,主要就是让你求出有最多结点的树的树叶: 我们只要利用并查集的知识吧所输入的数据连接 ...

  9. HDU 1856 More is better(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...

随机推荐

  1. Python day5 --------递归、匿名函数、高阶函数、内置函数

    一.递归 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 递归要求: 1. 必须有一个明确的结束条件 2. 每次进入更深一层递归时,问题规模相比上次递归都应有所减 ...

  2. idea log4j 用法

    1.导入jar包 这里用的maven导入 <!-- LOGGING begin --> <dependency> <groupId>org.slf4j</gr ...

  3. PostgreSQL(一)教程 -----高级特性

    一.视图 假设天气记录和城市为止的组合列表对我们的应用有用,但我们又不想每次需要使用它时都敲入整个查询.我们可以在该查询上创建一个视图,这会给该查询一个名字,我们可以像使用一个普通表一样来使用它: C ...

  4. CSUOJ 1170 A sample problem

    J: A Simple Problem Submit Page   Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 87     S ...

  5. 素数筛选法(prime seive)

    素数筛选法比较有名的,较常用的是Sieve of Eratosthenes,为古希腊数学家埃拉托色尼(Eratosthenes 274B.C.-194B.C.)提出的一种筛选法.详细步骤及图示讲解,还 ...

  6. python爬虫 Scrapy2-- 爬取豆瓣电影TOP250

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  7. spring JMS在接收消息的时候总是报错

    spring JMS在接收消息的时候总是报错 org.springframework.jms.UncategorizedJmsException: Uncategorized exception oc ...

  8. POJ - 2513 Colored Sticks(欧拉通路+并查集+字典树)

    https://vjudge.net/problem/POJ-2513 题解转载自:優YoU  http://user.qzone.qq.com/289065406/blog/1304742541 题 ...

  9. elasticsearch-dump 迁移es数据 (elasticdump)

    elasticsearch 部分查询语句 # 获取集群的节点列表: curl 'localhost:9200/_cat/nodes?v' # 列出所有索引: curl 'localhost:9200/ ...

  10. Ascending Rating(单调队列)

    题目描述 Before the start of contest, there are n ICPC contestants waiting in a long queue. They are lab ...