Hdoj 1856.More is better 题解

时间:2023-01-29 08:47:24

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.

Author

lxlcrystal@TJU

Source

HDU 2007 Programming Contest - Final


思路

裸并查集,只要找到所有分出来的集合当中元素最多的集合就行了,为此需要一个数组来记录个数,详见代码

代码

#include<bits/stdc++.h>
using namespace std;
int father[10000010];
int num[10000010];
int MaxValue;
void init(int n)
{
for(int i=1;i<=n;i++)
{
father[i]=i;
num[i] = 1;
}
}
int find(int x)
{
if(father[x]!=x) father[x] = find(father[x]);
return father[x];
}
void join(int a,int b)
{
int t1=find(a);
int t2=find(b);
if(t1!=t2)
{
father[t1]=t2;
num[t2] += num[t1];
MaxValue = max(num[t2],MaxValue);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
{
cout << 1 << endl;
continue;
}
init(10000000);
MaxValue = -1;
for(int i=1;i<=n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(find(a) != find(b)) join(a,b);
}
cout << MaxValue << endl;
}
return 0;
}

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

    转自:wutianqi http://www.wutianqi.com/?p=1069 tag:并查集 #include <iostream> using namespace std; # ...

  4. hdoj 1856 More is better【求树的节点数】

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

  5. Hdoj 1517&period;A Multiplication Game 题解

    Problem Description Stan and Ollie play the game of multiplication by multiplying an integer p by on ...

  6. Hdoj 1392&period;Surround the Trees 题解

    Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...

  7. Hdoj 1115&period;Lifting the Stone 题解

    Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...

  8. Hdoj 2108&period;Shape of HDU 题解

    Problem Description 话说上回讲到海东集团推选老总的事情,最终的结果是XHD以微弱优势当选,从此以后,"徐队"的称呼逐渐被"徐总"所取代,海东 ...

  9. Hdoj 1213&period;How Many Tables 题解

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

随机推荐

  1. POJ 1637 混合图的欧拉回路判定

    题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...

  2. SASS的一些使用体会(安装-配置-开启firefox的调试)

    对CSS预处理这个东西的看法,基本就是2种 第一种:不就是css吗,我会写就好了搞得那么复杂干嘛 第二种:感觉这个东西非常有必要,它规范了代码,使开发变得更轻松 好吧以前我是第一种,并且觉得又要配置环 ...

  3. Linux和Linux之间共享目录

    1.Linux 服务器端NFS服务器的配置 以root身份登陆Linux服务器,编辑/etc目录下的共享目录配置文件exports,指定共享目录及权限等. 执行如下命令编辑文件/etc/exports ...

  4. 第四十三篇、利用NSProxy解决NSTimer内存泄漏问题

    问题描述: 用NSTimer来实现每隔一定时间执行制定的任务,例如最常见的广告轮播图.如果我们在 timerWithTimeInterval:1 target:self 中指定target为当前控制器 ...

  5. 使用FOR循环语句在屏幕上输出一个由星号组成的直角三角形

    题目要求: 请用C++的信息输出方式,使用循环语句在屏幕上输出一个由星号组成的直角三角形,形状如下: * ** *** **** ***** 要求: 完全使用C++的信息输出方式,即cout以及流插入 ...

  6. Apache Zeppelin

    介绍 用于做数据分析和可视化 一.二进制安装 1)下载二进制包 wget http://mirrors.tuna.tsinghua.edu.cn/apache/incubator/zeppelin/0 ...

  7. flash&sol;flex 编译错误汇总

    来源:http://blog.chinaunix.net/uid-366408-id-116463.html 代码 消息 说明   1000 对 %s 的引用不明确. 引用可能指向多项.例如,下面使用 ...

  8. Elasticsearch head安装

    es5.0目前没有head插件所以不要下载错而是版本导致无法安装head; 安装head命令在es的bin目录下执行 ./plugin install mobz/elasticsearch-head ...

  9. 项目架构开发:数据访问层之Cache

    数据访问层简单介绍 数据访问层,提供整个项目的数据访问与持久化功能.在分层系统中所有有关数据访问.检索.持久化的任务,最终都将在这一层完成. 来看一个比较经典的数据访问层结构图 大概可以看出如下信息 ...

  10. stack&period;isEmpty()和empty()

    public class Stack<E> extends Vector<E> 可以看到Stack类继承了Vector类 这个是stack类里面的方法: /** * Tests ...