转自:wutianqi http://www.wutianqi.com/?p=1069

tag:并查集

#include <iostream>
using namespace std; #define MAX 10000001 // father[x]表示x的父节点
int father[MAX];
// rank[x]表示x的秩
int rank[MAX]; // 初始化
void Make_Set(int n)
{
for(int i=; i<=n; ++i)
{
father[i] = i;
rank[i] = ;
}
} // 查找
int Find_Set(int x)
{
if(x != father[x])
return Find_Set(father[x]);
return x;
} // 合并
void Union(int x, int y)
{
x = Find_Set(x);
y = Find_Set(y);
if(x == y) // x,y在同一个集合
return;
if(rank[x] > rank[y])
{
father[y] = x;
rank[x] += rank[y];
}
else if(rank[x] < rank[y])
{
father[x] = y;
rank[y] += rank[x];
}
else
{
father[x] = y;
//rank[y]++;
rank[y] += rank[x];
} } int main()
{
//freopen("input.txt", "r", stdin);
int a, b;
int nNum;
while(scanf("%d", &nNum) != EOF)
{
Make_Set(MAX);
if(nNum==)
{
printf("1\n");
continue;
}
for(int i=; i<nNum; ++i)
{
scanf("%d %d", &a, &b);
Union(a, b);
//printf("rank[%d]=%d rank[%d]=%d\n", a, rank[a], b, rank[b]);
}
int _max=;
for(int i=; i<=MAX; ++i)
//printf("%d ", rank[i]);
if(_max < rank[i])
_max = rank[i];
printf("%d\n", _max);
}
return ;
}

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【求树的节点数】

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  4. Hdoj 1856.More is better 题解

    Problem Description Mr Wang wants some boys to help him with a project. Because the project is rathe ...

  5. 杭电hdoj题目分类

    HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...

  6. HDOJ 题目分类

    HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:   ...

  7. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. Java中注解Annotation的定义、使用、解析

    此例子,用于说明如何在Java中对“注解 Annotation”的定义.使用和解析的操作.注解一般用于自定义开发框架中,至于为什么使用,此处不作过多说明,这里只说明如何使用,以作备记.下面例子已测试, ...

  2. 将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m

    给定两个数m,n,其中m是一个素数. 将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m. 输入 第一行是一个整数s(0<s<=100),表示测试数据的组数 随后 ...

  3. DropDownList另一种写法

    2013-09-29 17:04:47 1.性别: <asp:DropDownList ID="DrpSex" runat ="server"  Widt ...

  4. python 实现梯度下降

    在多元线性回归中会用到梯度下降来计算参数值.这里我用python实现一个梯度下降版本. 这里多元线性方程为 y = A0+A1*x1+...+An* xn 数据输入格式,y表示 y \t x1 \t ...

  5. javascript给不能修改的函数增加额外处理的方法

    不知道是否可行,但姑且google之. 查看到相关的stack overflow(堆栈溢出)帖子:http://stackoverflow.com/questions/1659219/add-to-a ...

  6. SharedSDK微信分享不成功,分享之后没有反应

    对于一般来说,使用SharedSDK的时候,分享不成功不外乎下面几个原因: 1.测试没有打包2.打包的keystore跟微信开放平台上面的不一致, 导致MD5码不一致3.分享参数错误4.应用没有审核通 ...

  7. 杀死进程 kill -9

    cui@bug:~$ killall -h 用法: killall [选项]... [--] 进程名... killall -l, --list killall -V, --version -e,-- ...

  8. callback调用测试

    <html> <head> <script> var context="全局"; var testObj={ context:"初始& ...

  9. elr_memory_pool详解

    Preface Usually, memory allocation of OS is fast, especially the computer has just started. But over ...

  10. nginx反向代理的配置优化

    作者:守住每一天 blog:liuyu.blog.51cto.combbs:bbs.linuxtone.orgmsn:liuyubj520#hotmail.comemail:liuyu105#gmai ...