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

HDU 2007 Programming Contest - Final


思路

裸并查集,只要找到所有分出来的集合当中元素最多的集合就行了,为此需要一个数组来记录个数,详见代码

代码

#include<bits/stdc++.h>
using namespace std;
int father[10000010];
int num[10000010];
int MaxValue;
void init(int n)
{
for(int i=1;i<=n;i++)
{
father[i]=i;
num[i] = 1;
}
}
int find(int x)
{
if(father[x]!=x) father[x] = find(father[x]);
return father[x];
}
void join(int a,int b)
{
int t1=find(a);
int t2=find(b);
if(t1!=t2)
{
father[t1]=t2;
num[t2] += num[t1];
MaxValue = max(num[t2],MaxValue);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
{
cout << 1 << endl;
continue;
}
init(10000000);
MaxValue = -1;
for(int i=1;i<=n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(find(a) != find(b)) join(a,b);
}
cout << MaxValue << endl;
}
return 0;
}

Hdoj 1856.More is better 题解的更多相关文章

  1. 并查集(HDOJ 1856)

    并查集   英文:Disjoint Set,即“不相交集合” 将编号分别为1…N的N个对象划分为不相交集合, 在每个集合中,选择其中某个元素代表所在集合. 常见两种操作: n       合并两个集合 ...

  2. HDOJ 1856

    #include<cstdio> #include<cstdlib> typedef struct ufse *ufset; struct ufse { ]; ]; }UFS; ...

  3. HDOJ 1856 More is better

    转自:wutianqi http://www.wutianqi.com/?p=1069 tag:并查集 #include <iostream> using namespace std; # ...

  4. hdoj 1856 More is better【求树的节点数】

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  5. Hdoj 1517.A Multiplication Game 题解

    Problem Description Stan and Ollie play the game of multiplication by multiplying an integer p by on ...

  6. Hdoj 1392.Surround the Trees 题解

    Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...

  7. Hdoj 1115.Lifting the Stone 题解

    Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...

  8. Hdoj 2108.Shape of HDU 题解

    Problem Description 话说上回讲到海东集团推选老总的事情,最终的结果是XHD以微弱优势当选,从此以后,"徐队"的称呼逐渐被"徐总"所取代,海东 ...

  9. Hdoj 1213.How Many Tables 题解

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

随机推荐

  1. ARC 066D Xor Sum AtCoder - 2272 (打表找规律)

    Problem Statement You are given a positive integer N. Find the number of the pairs of integers u and ...

  2. git在vs2017中的使用

    对于习惯了右键提交源代码的道友来说,敲命令行真的蓝瘦香菇.所幸17里集成了Git插件,用起来还是挺方便的. 1.本地安装git,工具还是要有的,主要用于配置环境,ssh配置一下.就不用每次都去连接了. ...

  3. 2 JAVA 项目名称前红色叹号如何解决

    1 Java 项目前出现红色叹号Eclipse找不到项目需要的JAR包,可以在这里面解决: ① 右键点击项目,选择[Build Path].[Configure Build Path...] ② 在这 ...

  4. Java 多线程概述

    几乎所有的操作系统都支持同时运行多个任务,一 个任务通常就是一个程序,每个运行中的程序就是一个进程.当一个程序运行时,内部可能包含了多个顺序执行流,每个顺序执行流就是一个线程. 线程和进程 几乎所有的 ...

  5. API知识点总结

    一.开发api接口开放给其他人调用的api接口(短信接口,支付宝api) 二.api安全弱点数据窃取(解决加密),数据篡改(解决MD5),数据泄露(爬虫技术)(解决令牌)1.加密(HTTPS传输-收费 ...

  6. Vue2.0 子组件和父组件之间的传值

    Vue是一个轻量级的渐进式框架,对于它的一些特性和优点在此就不做赘述,本篇文章主要来探讨一下Vue子父组件通信的问题 首先我们先搭好开发环境,我们首先得装好git和npm这两个工具(如果有不清楚的同学 ...

  7. zabbix添加监控Mysql

    起因:zabbix自带的mysql监控模板直接使用会显示“不支持的”因为key的值是通过Mysql用户查看"show global status"信息或者用mysqladmin命令 ...

  8. hive 查询注意问题

    1)对于hive内置的列,不是自己建的,在查询的时候需要添加反引号` 比如:`_mt_message`,别在这里犯错误, (2)南京的_mt_message是json的格式,所以可以直接使用:get_ ...

  9. python设计模式第十天【观察者模式】

    1.应用场景 (1)监听事件驱动程序中的外部事件 (2)监听某个对象的状态变化 (3)发布-订阅模型中,消息出现时通知邮件列表中的订阅者 2. 观察者模式UML图 3. 代码实现: #!/usr/bi ...

  10. Yii2控制台程序最佳实践

    模板工程标准的控制台程序要素: (1)完整明确文字提示用户(并且使用红,绿,黄三种颜色标识提示文字:红色为错误相关,绿色为成功相关,黄色为进行中提示) (2)告知用户运行进度(完成任务的一部分即显示进 ...