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.

 
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <set>
#include <algorithm>
using namespace std;
#define Maxn 10000005

int father[Maxn];
int num[Maxn];

void init ()
{
    for(int i = 1; i <= Maxn; i++)
    {
        father[i] = i;
        num[i] = 1;
    }
}

int find(int x)
{
    /*if (x != father[x])
    {
        return x = find(father[x]);//压缩路径有问题,TLE好几发
    }*/
    if(father[x]!=x)
        father[x]=find(father[x]);
    return father[x];
}

void mix(int x,int y)
{
    int fx = find(x);
    int fy = find(y);
    if (fx != fy)
    {
        father[fx] = fy;
        num[fy] += num[fx];
    }
}

int main()
{
    int fuck;
    while(~scanf("%d",&fuck))
    {
        if (fuck == 0)
        {
            printf("1\n");
            continue;
        }
        init();
        int a,b;
        int big = 0;
        for(int i = 0; i < fuck; i++)
        {
            scanf("%d%d",&a,&b);
            // big = find_max(big,a,b);
            mix(a,b);
            if (a > big)
            {
                big = a;
            }
            if (b > big)
            {
                big = b;
            }
        }
        int maxn = 0;
        for(int i = 1; i <= big; i++)
        {
            if (maxn < num[i])
            {
                maxn = num[i];
            }
        }
        printf("%d\n",maxn);
    }
}

  

 
 

hud1856 并查集的更多相关文章

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

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

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

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

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

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

  4. bzoj1854--并查集

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

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

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

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

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

  7. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

  8. Codeforces 731C Socks 并查集

    题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...

  9. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

随机推荐

  1. java_泛型(2016-11-17)

    没有自己敲,这篇博客讲的不错,直接记录. 犯懒啊 重点关注 T,?以及擦除 Java总结篇系列:Java泛型:http://www.cnblogs.com/lwbqqyumidi/p/3837629. ...

  2. printf 输出格式

    printf 输出格式C中格式字符串的一般形式为: %[标志][输出最小宽度][.精度][长度]类型,其中方括号[]中的项为可选项.各项的意义介绍如下:1.类型类型字符用以表示输出数据的类型,其格式符 ...

  3. thinkphp中表有前缀名的时候申明模板的方法

    $Model=new \Home\Model\Stu_activityModel; $Model=D('Stu_activity');//错误的声明不了

  4. Android中WebView的JavaScript代码和本地代码交互的三种方式

    一.Android中WebView的漏洞分析最近在开发过程中遇到一个问题,就是WebView使用的时候,还是需要解决之前系统(4.2之前)导致的一个漏洞,虽然现在这个系统版本用户很少了,但是也不能忽视 ...

  5. 如何设置路由器实现静态IP配置

    一.概述 嵌入式开发者,经常面对这样的环境:PC(windows)+虚拟机(linux)+开发板.我们希望三者都能相互通信,而且可以联网. 对于实验室只提供一根网线,而自己没有额外的增加端口数量的设备 ...

  6. io开发之C语言第二天

    开发环境是OS X系统下的Xcode Xcode的两个快捷键以及打开Xcode项目的正确方式 快捷键:command + B 编译 + 链接 快捷键:command + R 编译 + 链接 + 运行 ...

  7. jdbc 安装驱动

    在使用JDBC编程时需要连接数据库,导入JAR包是必须的,导入其它的jar包方法同样如此,导入的方法是 打开eclipse 1.右击要导入jar包的项目,点properties 2.左边选择java ...

  8. 使用token机制来验证用户的安全性-b

    登录的业务逻辑{    http:是短连接.         服务器如何判断当前用户是否登录?        // 1. 如果是即时通信类:长连接.    // 如何保证服务器跟客户端保持长连接状态? ...

  9. IOS 上传头像-b

    感谢大神分享 1.首先,后台给了我这样的接口 1-后台数据接口 2.首先加上代理方法 <UIActionSheetDelegate,UINavigationControllerDelegate, ...

  10. hadoop的一些重要配置参数

    hadoop集群管理内存设置 Mapreduce内存使用设置 hadoop job重要性能参数