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_jdbc_oracle简单总结(2016-11-23)

    JDBC连接oracle的实例 好久没写过jdbc,基本忘干净了,随意插一个图,简单学习一下.然后干别的..... 使用jdbc操作数据库步骤是固定的 1.将驱动包导入到数据库,每一个数据库的驱动包都 ...

  2. LuaNode 开源库

    受CJSON库的启发,用C++实现解析Lua文件的库. 整个库大概800行代码,因为新鲜出炉,所以有些潜在问题尚未发现. 截图中包含使用例子. 以下接口清单: LuaNode(); LuaNode(c ...

  3. php利用时间生成随机函数

    date("YmdHis",time()); rand();    生成随机数   当括号内无参数时 系统会以当前时间为种子进行随机数的生成 rand(1,10);  括号里面是生 ...

  4. 关于Zen Coding:css,html缩写

    zen coding 是一个俄罗斯人写的编辑器(支持大部分现下流行的编辑器)插件,其安装也是非常简单,只要安装插件,然后在项目中拷贝js文件就可以.像Webstorm6.0.2中已经包含这样的插件.什 ...

  5. Mac下修改Mysql密码

    1. 停止Mysql 2. cd /usr/local/mysql/bin/ 3. 使用跳过权限方式启动mysql sudo ./mysqld_safe --skip-grant-tables &am ...

  6. 使用OPCDAAuto.dll编写C# OPC采集程序

    在一台新机器上运行使用OPC自动化接口编写的C#程序报错如下: 索 COM 类工厂中 CLSID 为 {28E68F9A-8D75-11D1-8DC3-3C302A000000} 的组件失败,原因是出 ...

  7. http server v0.1_http_server.c

    /**************************************************************** filename: http_server.c author: xx ...

  8. redis问题解决

    一, redis的奇葩问题:我使用命令 redis-cli shutdown 关闭redis之后就再也灭洋启动了! 尝试1: 使用命令 sudo /etc/init.d/redis-server st ...

  9. .NET framework Chart组件SeriesChartType 枚举

      成员名称 说明   Area 面积图类型.   Bar 条形图类型.   BoxPlot 盒须图类型.   Bubble 气泡图类型.   Candlestick K 线图类型.   Column ...

  10. 无插件Vim编程技巧

    无插件Vim编程技巧 http://bbs.byr.cn/#!article/buptAUTA/59钻风 2014-03-24 09:43:46 发表于:vim  相信大家看过<简明Vim教程& ...