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. 前端开发构建工具gulp的安装使用

    曾几何时还在使用grunt作为前端的构建工具,直到有一天同事向我推荐了gulp,在这里博主将不讨论gulp与grunt各自优势的比较,只为大家介绍gulp如何安装和使用. Gulp 是用 nodejs ...

  2. 图像显示 imshow()[OpenCV 笔记5]

    void imshow(const string& winname InputArray mat); winname 窗口表识名称 mat 需要显示的图像.InputArray类型,声明如下 ...

  3. 水题(素数表)NYOJ素数距离

                描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度素数,则输出左侧的值及相应距离. 如果输入的整数本身就是素数,则输 ...

  4. React Native:使用 JavaScript 构建原生应用 详细剖析

    数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生的 iOS 应用——就在今天,Beta 版的仓库释出了! 基于 Pho ...

  5. phpexcel的写出操作(生成excel表)

    /* thinkphp3.2 ThinkPHP/Library/Org/Util/ 放置位置 http://files.cnblogs.com/files/youligai/phpexcel.zip  ...

  6. session与cookie的区别,有哪些不同之处

    session与cookie的区别,根据自己的理解总结如下: (1)cookie是一种客户端的状态管理技术,将状态写在 浏览器端,而session是一种服务器端的状态管理技术,将 状态写在web服务器 ...

  7. HTTP 无法注册 URL http://+:80/Temporary_Listen_Addresses/92819ef8-81ea-4bd9-

    今天在练习wcf时,客户端调用服务端方法时出现异常.如下: 未处理System.ServiceModel.AddressAlreadyInUseException Message="HTTP ...

  8. Python 基础-python-列表-元组-字典-集合

    列表格式:name = []name = [name1, name2, name3, name4, name5] #针对列表的操作 name.index("name1")#查询指定 ...

  9. Coursera《machine learning》--(8)神经网络表述

    本笔记为Coursera在线课程<Machine Learning>中的神经网络章节的笔记. 八.神经网络:表述(Neural Networks: Representation) 本节主要 ...

  10. 自己挖坑自己跳 之JsonMappingException: (was java.lang.NullPointerException) (through reference chain:)

    在Web项目中,我们经常会设计一些与界面相对应的JavaBean作为Entity,而为了兼容前台传入的空值,有些字段我们会用包装类型而不是基本类型.可是往往我的Entity已经设计完成,很多时候我们会 ...