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. 批量采集世纪佳缘会员图片及winhttp异步采集效率

    原始出处:http://www.cnblogs.com/Charltsing/p/winhttpasyn.html 最近老有人问能不能绕过世纪佳缘的会员验证来采集图片,我测试了一下,发现是可以的. 同 ...

  2. java 8中抽象类与接口的异同

    1.java 8中抽象类与接口的异同 相同点: 1)都是抽象类型: 2)都可以有实现方法(以前接口不行): 3)都可以不需要实现类或者继承者去实现所有方法,(以前不行,现在接口中默认方法不需要实现者实 ...

  3. Django 中的Form、ModelForm

    一.ModelForm 源码 class ModelForm(BaseModelForm, metaclass=ModelFormMetaclass): pass def modelform_fact ...

  4. 福州大学软件工程1816 | W班 第4次作业(团队展示)成绩排名

    作业链接 评分细则 队员姓名与学号(标记组长),其中4-7人一组,特殊情况经老师允许后可以突破限制:(1分) 队名(体现项目内容,并要求有亮点与个性):(1分) 拟作的团队项目描述:一句话(中英文不限 ...

  5. Eclipse Todo Tasks 任务试图

    java - Find TODO tags in Eclipse - Stack Overflowhttps://stackoverflow.com/questions/16903046/find-t ...

  6. Java中的break,continue关于标签的用法(转载)

    Java的控制循环结构中是没有关键字goto的,这种做法有它的好处,它提高了程序流程控制的可读性,但是也有不好的地方,它降低了程序流程控制的灵活性,所以说,“上帝是公平的”.所以,Java为了弥补这方 ...

  7. [转帖]firewall-cmd

    firewall-cmd https://wangchujiang.com/linux-command/c/firewall-cmd.html 高手大作 等哪天需要防火墙了 再练习一下. Linux上 ...

  8. Java8 Stream实例--统计出所有含‘张’字的人员的平均年龄

    package com.zhangxueliang.demo; import java.util.ArrayList; import java.util.List; import java.util. ...

  9. JS 类型检测

    typeof 适合函数对象和基本类型的判断 typeof 100instanceof 适合判断对象类型 obj instanceof Object 基于原型链判断操作符,若做操作符不是对象,则会直接返 ...

  10. yum仓库搭建

    1. 创建yum仓库目录 mkdir -p /application/yum/centos6.6/x86_64/ cd /application/yum/centos6.6/x86_64/ rz  # ...