More is better

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)

Total Submission(s): 19011    Accepted Submission(s): 6998

Problem Description
Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.



Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are
still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
 
Input
The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)
 
Output
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep. 
 
Sample Input
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
 
Sample Output
4
2
Hint
A and B are friends(direct or indirect), B and C are friends(direct or indirect),
then A and C are also friends(indirect). In the first sample {1,2,5,6} is the result.
In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.
 
Author
lxlcrystal@TJU
 
Source
 
Recommend
 

pid=1856" style="color:rgb(26,92,200); text-decoration:none">Statistic | Submit | Discuss | Note

一个并查集 计算每一个集合的元素 找出元素最多的那个集合,输出元素的个数

输入n=0时也应该输出1

难点就在于。怎么计算集合的元素个数。。事实上仅仅要在初始的时候每一个元素都初始为1,

然后合并集合的时候+1就好了

#include <stdio.h>
#include <string.h>
#define N 10000000+5
int fa[N],stamp[N];
int find(int x)
{
if(fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
}
void init()
{
for(int i=1;i<=N;i++)
fa[i]=i,stamp[i]=1;
}
int main()
{
int n,min,max;
while(~scanf("%d",&n))
{
init();
min=10000000+5,max=-1;
for(int i=0;i<n;i++)
{
int a,b;
scanf("%d %d",&a,&b);
a=find(a),b=find(b);
if(a!=b)
fa[a]=b,stamp[b]+=stamp[a];
if(a>max) max=a;
if(b>max) max=b;
if(a<min) min=a;
if(b<min) min=b;
}
int count=1;
for(int i=min;i<=max;i++)
{
if(stamp[i]>count)
count=stamp[i];
}
printf("%d\n",count);
}
return 0;
}

hdu1856 More is better (并查集)的更多相关文章

  1. 【HDU1856】More is better(并查集基础题)

    裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. #include <iostream&g ...

  2. hdu-1856 More is better---带权并查集

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1856 题目大意: 一个并查集 计算每个集合的元素 找出元素最多的那个集合,输出元素的个数 解题思路: ...

  3. 并查集(Union-Find) 应用举例 --- 基础篇

    本文是作为上一篇文章 <并查集算法原理和改进> 的后续,焦点主要集中在一些并查集的应用上.材料主要是取自POJ,HDOJ上的一些算法练习题. 首先还是回顾和总结一下关于并查集的几个关键点: ...

  4. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  5. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  6. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  7. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  8. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  9. [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

随机推荐

  1. Delphi的时间 x87 fpu control word 精度设置的不够

    在win7 64位系统下, 一个DELPHI写的DLL注入一个C语言程序后. 出现非常奇怪的浮点数相加出错的情况. (注: 在XP系统下是正常的).比如: 40725.0001597563 + 0.7 ...

  2. node03--http

    form.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  3. TortoiseGit配合msysGit在Git@OSC代码托管的傻瓜教程

    命令行太麻烦,肿么破?便便利用睡觉的时间解决了一点效率问题,tortoiseGit处理GitHub,一样可以处理 Git @osc ,虽然说可以用gitk来调出图形界面,but,我就是不想看见黑黑的命 ...

  4. HibernateProperties 配置属性

    Hibernate properties Hibernate配置属性 属性名 用途hibernate.dialect ;一个Hibernate Dialect类名允许Hibernate针对特定的关系数 ...

  5. 运维派 企业面试题3 为上题中的 "十个随机字母_test.html" 文件 更名

    Linux运维必会的实战编程笔试题(19题) 企业面试题3 #将试题2中创建的文件名uopiyhgawe_test.html# test-->修改为omg,html-->HTML 方法一: ...

  6. Linux 图形文件压缩/解压缩实用程序,归档管理器。

    1.ArkArk是KDE桌面环境默认的归档管理器,支持插件设置,允许你创建一个压缩包,查看压缩文件的内容,解压压缩包的内容到你所选定的目录.它能处理多种格式,包括 tar.gzip.bzip2.zip ...

  7. Unity C# 设计模式(二)简单工厂模式

    定义: 简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一. 简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例 ...

  8. STM32中断名词

    1.NVIC的优先级概念    占先式优先级 (pre-emption priority):    高占先式优先级的中断事件会打断当前的主程序/中断程序运行— —抢断式优先响应,俗称中断嵌套.    ...

  9. 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A】 Palindromic Supersequence

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 字符串倒着加到原串右边就好 [代码] #include <bits/stdc++.h> using namespace ...

  10. 一 SSH 无密码登陆 & Linux防火墙 & SELinux关闭

    如果系统环境崩溃.   调用/usr/bin/vim /etc/profile   SHH无密码登陆 所有要做得节点上运行   修改 host name vi /etc/sysconfig/netwo ...