More is better

Time Limit:1000MS     Memory Limit:102400KB     64bit IO Format:%I64d & %I64u

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.
         

#include<stdio.h>
#include<algorithm>
using namespace std;
int p[] , rk[] , maxn ;
int m ; void init ()
{
for (int i = ; i <= ; i++) {
p[i] = i ;
rk[i] = ;
}
maxn = ;
} int find (int x)
{
return x == p[x] ? x : p[x] = find (p[x]) ;
} void Union (int x , int y)
{
p[x] = y ;
rk[y] += rk[x] ;
maxn = max (maxn , rk[y]) ;
rk[x] = ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
int x , y ;
while (~ scanf ("%d" , &m) ) {
init () ;
for (int i = ; i < m ; i++ ) {
scanf ("%d%d" , &x , &y ) ;
x = find (x) ;
y = find (y) ;
if (x != y)
Union ( x , y ) ;
}
if (maxn )
printf ("%d\n" , maxn ) ;
else
printf ("1\n") ;
}
return ;
}
 

More is better(MST)(求无向图中最大集合元素个数)的更多相关文章

  1. 蓝桥杯历届试题 危险系数(dfs或者并查集求无向图关于两点的割点个数)

    Description 抗日战争时期,冀中平原的地道战曾发挥重要作用. 地道的多个站点间有通道连接,形成了庞大的网络.但也有隐患,当敌人发现了某个站点后,其它站点间可能因此会失去联系. 我们来定义一个 ...

  2. 萌新笔记——Cardinality Estimation算法学习(一)(了解基数计算的基本概念及回顾求字符串中不重复元素的个数的问题)

    最近在菜鸟教程上自学redis.看到Redis HyperLogLog的时候,对"基数"以及其它一些没接触过(或者是忘了)的东西产生了好奇. 于是就去搜了"HyperLo ...

  3. 求n!中因子k的个数

    思路: 求n的阶乘某个因子k的个数,如果n比较小,可以直接算出来,但是如果n很大,此时n!超出了数据的表示范围,这种直接求的方法肯定行不通.其实n!可以表示成统一的方式. n!=(km)*(m!)*a ...

  4. Cardinality Estimation算法学习(一)(了解基数计算的基本概念及回顾求字符串中不重复元素的个数的问题)

    最近在菜鸟教程上自学redis.看到Redis HyperLogLog的时候,对“基数”以及其它一些没接触过(或者是忘了)的东西产生了好奇. 于是就去搜了“HyperLogLog”,从而引出了Card ...

  5. 求数组中最小的k个数

    题目:输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. package test; import java.util.Arra ...

  6. 求n!中含有某个因子个数的方法

    链接 [https://www.cnblogs.com/dolphin0520/archive/2011/04/11/2012891.html]

  7. poj 2406 求字符串中重复子串的个数

    Sample Input abcdaaaaababab.Sample Output 1 //1个abcd4 //4个a3 //3个ab #include<stdio.h> #include ...

  8. php 统计一维数组中重复的元素个数

    <?php echo "<pre>"; $array = array(1, 1, 1, 54, 3,4, 3,4, 3, 14, 3,4, 3,7,8,9,12, ...

  9. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

随机推荐

  1. java之hashCode

    package com.simope.myTest; import java.util.HashMap; import java.util.Map; public class Test20151022 ...

  2. -webkit-overflow-scrolling:touch iosBug

    IOS8+  -webkit-overflow-scrolling:touch 会导致webview崩溃 解决方案 用js动态添加样式  比如: $("body").css(&qu ...

  3. 获取url据对路径写法

    var _absUrl = (function () { var a; return function (url) { if (!a) a = document.createElement('a'); ...

  4. mysql使用基础 sql语句(一)

    csdn博文地址:mysql使用基础 sql语句(一)  点击进入 命令行输入mysql -u root -p,回车再输入密码,进入mysql. 终端命令以分号作为一条语句的结束,可分为多行输入,只需 ...

  5. 简单的3个SQL视图搞定所有SqlServer数据库字典

    网上有很多SQL SERVER数据库字典的SQL语句,七零八落,我在工作整理了一下思路,总结SQL代码如下.数据库字典包括表结构(分2K和2005).索引和主键.外键.约束.视图.函数.存储过程.触发 ...

  6. ThinkPHP中简单的CURD操作

    前言 我们通过一个简答例子来简述CURD的操作.首先看一下数据库的样子,其中id为自增行,其它是varchar 一.查询操作 首先,创建在Controller文件夹下创建一个User控制器,在该控制器 ...

  7. 第二章:javascript: 数组

    数组是编程世界里最常见的数据结构.任何一种编程语言都包含数组,只是形式稍微有差异.数组是编程语言中的内建类型,通常效率都很高.可以满足不同需求的数据存储,本章将探索javascript中的数组工作原理 ...

  8. 虚拟机 vlan trunk 特性

    1. 功能 1)允许不同vlan的network下的虚拟机之间通信.一般情况下,虚拟机只能在相同vlan的网络下通信. 2)允许虚拟机发送vlan报文. 2. 组网图 虚拟机出来的tap设备连接到tb ...

  9. Lucene -- 实时索引

    lucene的实时搜索可以分成:实时和近实时的搜索. 实时只能依靠内存了. 近实时可以用lucene中提供org.apache.lucene.index.DirectoryReader.open(In ...

  10. Oracle 11g 默认用户名和密码

    安装ORACLE时,若没有为下列用户重设密码,则其默认密码如下: 用户名 / 密码                      登录身份                              说明 ...