More is better

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 17683    Accepted Submission(s):
6493

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<string.h>
#define MAX 10000010
#define maxn(a,b)(a>b?a:b)
int set[MAX],path[MAX];
int a[100010],b[100010];
int sum=0;
int find(int fa)
{
int t;
int ch=fa;
while(fa!=set[fa])
fa=set[fa];
while(ch!=fa)
{
t=set[ch];
set[ch]=fa;
ch=t;
}
return fa;
}
void mix(int x,int y)
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
{
set[fx]=fy;
path[fy]+=path[fx];
if(sum<path[fy])
sum=path[fy];
}
}
int main()
{
int n,m,j,i,max,point;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
{
printf("1\n");
continue;
}
max=0;
for(i=1;i<=n;i++)
{
scanf("%d%d",&a[i],&b[i]);
if(max<maxn(a[i],b[i]))
max=maxn(a[i],b[i]);
}
for(i=1;i<=max;i++)
{
set[i]=i;
path[i]=1;
}
for(i=1;i<=n;i++)
{
mix(a[i],b[i]);
}
printf("%d\n",sum);
sum=0;
}
return 0;
}

  

hdoj 1856 More is better【求树的节点数】的更多相关文章

  1. Java实现二叉搜索树的添加,前序、后序、中序及层序遍历,求树的节点数,求树的最大值、最小值,查找等操作

    什么也不说了,直接上代码. 首先是节点类,大家都懂得 /** * 二叉树的节点类 * * @author HeYufan * * @param <T> */ class Node<T ...

  2. hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  3. 并查集(HDOJ 1856)

    并查集   英文:Disjoint Set,即“不相交集合” 将编号分别为1…N的N个对象划分为不相交集合, 在每个集合中,选择其中某个元素代表所在集合. 常见两种操作: n       合并两个集合 ...

  4. poj2631 求树的直径裸题

    题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...

  5. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

  6. poj3107 求树的重心(&& poj1655 同样求树的重心)

    题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...

  7. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

  8. [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)

    http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...

  9. 求树的重心(POJ1655)

    题意:给出一颗n(n<=2000)个结点的树,删除其中的一个结点,会形成一棵树,或者多棵树,定义删除任意一个结点的平衡度为最大的那棵树的结点个数,问删除哪个结点后,可以让平衡度最小,即求树的重心 ...

随机推荐

  1. oh my zsh命令

    打开某个文件夹地址,输入 cdf   命令,会自动进入这个文件夹命令行 open ./      打开当前命令行所在目录的文件夹

  2. android 自动调整屏幕分辨率

    请看 http://blog.csdn.net/awp258/article/details/7593340

  3. 设置一个POJO的某个属性的默认值

    //月利率private BigDecimal monthRate=new BigDecimal(0);  

  4. 函数page_align

    /************************************************************//** Gets the start of a page. @return ...

  5. POJ 1815 Friendship ★(字典序最小点割集)

    [题意]给出一个无向图,和图中的两个点s,t.求至少去掉几个点后才能使得s和t不连通,输出这样的点集并使其字典序最大. 不错的题,有助于更好的理解最小割和求解最小割的方法~ [思路] 问题模型很简单, ...

  6. php网页显示正方形图片缩略图

    需求是这样的:原始图片的大小是不定的,类似800*600.1000*756,现有一个页面要以正方形(60*60)显示这些图片,注意:图片只能在内存处理,不能缩小后保存到本地磁盘. 解决办法: html ...

  7. nginx.conf配置

    在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户 user www-data; #启动进程,通常设置成和cpu的数量相等 worker_processe ...

  8. linux 下使用 cmake安装mysql

    原文地址:http://www.cppblog.com/issay789/archive/2013/01/05/196967.html 一.安装 m4 下载地址: http://files.w3pc. ...

  9. 【Jenkins】Linux搭建Jenkins平台

    为了配合上一篇的ant+jenkins做持续集成,需要在linux环境下搭建一个jenkins平台.网上有很多安装的例子,我主要记录一下自己遇到的问题,真真的是特别惆怅的,每次我遇到的问题都格外多. ...

  10. HDU 5690 All X 暴力循环节

    分析:暴力找循环节就好了 #include <iostream> #include <cstdio> #include <cstdlib> #include < ...