转自: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. Log4Net总结

    Log4Net是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件.数据库.EventLog等),日志就是程序的黑匣子,可以通过日志查看系统的运行过程,从而发现系统的问题.日志的作用:将运 ...

  2. Android BroadcasetReceiver

    ci@clinux:~/mp_mtk$ adb -s QGAEFYAAOFN7PNOB logcat PhoneReceiver:e *:S --------- beginning of system ...

  3. 如何设置SecureCRT通过代理连接SSH[转]

    http://blog.didu.me/article/84 公司限制了连接外网的端口和IP,只能通过proxy 连接.刚配置了一下 secureCRT 连接外网,貌似速度还是不错,写出来共享下. 如 ...

  4. 【转载】DataGridView 使用集合作为数据源,并同步更新

    原文地址:http://hi.baidu.com/netyro/item/7340640e36738a813c42e239 今天做项目时遇到一个挠头的问题,当DataGridView的数据源为泛型集合 ...

  5. 腾讯微博OAuth2.0 .NET4.0 SDK 发布以及网站腾讯微博登陆示例代码(原创)

    1.使用简单方便,包含详细注释: 2.暂时只支持xml格式字符串的转换,建议接口使用xml参数:3.QweiboSDK.Controllers命名空间下已包含所有API接口:4.只需调用到Qweibo ...

  6. Waring:This LinearLayout layout or its FrameLayout parent is useless; transfer the background attribute to the other view

    解决方法请参考: You have a single component (row) vertical linear layout, containing another linear layout. ...

  7. python:执行一个命令行N次

    经常希望可以执行一个命令行N次...windows下没有现成的工具(有?推荐给我!) 用python写一个... #!/usr/bin/evn python #coding: utf-8 " ...

  8. C string 函数大全

    PS:本文包含了大部分strings函数的说明,并附带举例说明.本来想自己整理一下的,发现已经有前辈整理过了,就转了过来.修改了原文一些源码的问题,主要是用char *字义字符串的问题,导致程序运行时 ...

  9. c语言之fopen参数(r+,w+,a+)

    经查找一些资料,并且亲自实验之后得出: r+:打开已存在的文件时,光标位于开头,文件可读,可写, 写数据时,由于光标位于开头,所以会覆盖原有数据 w+:打开的文件已存在时,将覆盖原文件 a+:打开已存 ...

  10. 相同的 birthday

    Description Sometimes some mathematical results are hard to believe. One of the common problems is t ...