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. weblogic集群无法启动,提示java.lang.NumberFormatException

    我有两台weblogic9.2做的集群A,B,A是主服务器,B是受管服务器,后来通过脚本启动weblogic服务,A服务启动异常,经查后台的日志文件发现报错消息如下: WebLogic Server ...

  2. 使用intellij idea搭建MAVEN+springmvc+mybatis框架

    原文:使用intellij idea搭建MAVEN+springmvc+mybatis框架 1.首先使用idea创建一个maven项目 2.接着配置pom.xml,以下为我的配置 <projec ...

  3. linux进程模型总结

    Linux进程通过一个task_struct结构体描述,在linux/sched.h中定义,通过理解该结构,可更清楚的理解linux进程模型.       包含进程所有信息的task_struct数据 ...

  4. “WIZnet杯”以太网技术竞赛即将开始!

  5. poj 1328 Radar Installation(贪心)

    题目:http://poj.org/problem?id=1328   题意:建立一个平面坐标,x轴上方是海洋,x轴下方是陆地.在海上有n个小岛,每个小岛看做一个点.然后在x轴上有雷达,雷达能覆盖的范 ...

  6. Notepad++加上xml格式化的功能

    工作中需要用代码创建一个XML文件,创建完发现XML内容都处在同一行,导致非常不容易查看清楚XML代码.于是习惯性地用 Notepad++查看,想用它来对XML代码格式化一下. 于是找到了TextFX ...

  7. storm - 使用过程中的一点思考

    引子 这几天为了优化原有的数据处理框架,比较系统的学习了storm的一些内容,整理一下心得 1. storm提供的是一种数据处理思想,它不提供具体的解决方案 storm的核心是topo的定义,而top ...

  8. CodeForces Round #290 Div.2

    A. Fox And Snake 代码可能有点挫,但能够快速A掉就够了. #include <cstdio> int main() { //freopen("in.txt&quo ...

  9. [scu 4423] Necklace

    4423: Necklace Description baihacker bought a necklace for his wife on their wedding anniversary. A ...

  10. ASP.NET程序中动态修改web.config中的设置项目(后台CS代码)

    using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Dra ...