hdoj 1856 More is better【求树的节点数】
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
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.
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)
equals to the maximum number of boys Mr Wang may keep.
2
A and B are friends(direct or indirect), B and C are friends(direct or indirect),
#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【求树的节点数】的更多相关文章
- Java实现二叉搜索树的添加,前序、后序、中序及层序遍历,求树的节点数,求树的最大值、最小值,查找等操作
什么也不说了,直接上代码. 首先是节点类,大家都懂得 /** * 二叉树的节点类 * * @author HeYufan * * @param <T> */ class Node<T ...
- hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
- 并查集(HDOJ 1856)
并查集 英文:Disjoint Set,即“不相交集合” 将编号分别为1…N的N个对象划分为不相交集合, 在每个集合中,选择其中某个元素代表所在集合. 常见两种操作: n 合并两个集合 ...
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- poj3107 求树的重心(&& poj1655 同样求树的重心)
题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...
- hdu 4607 Park Visit 求树的直径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...
- [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...
- 求树的重心(POJ1655)
题意:给出一颗n(n<=2000)个结点的树,删除其中的一个结点,会形成一棵树,或者多棵树,定义删除任意一个结点的平衡度为最大的那棵树的结点个数,问删除哪个结点后,可以让平衡度最小,即求树的重心 ...
随机推荐
- 【转】VC++消息钩子编程
VC++消息钩子编程
- PHP实现浏览历史记录
http://www.3a88.com/service/206.html http://www.1360.cc/ZhanChangJiaoCheng/6831.html http://www.osch ...
- 约瑟夫环问题-循环链表VS数组
2013-08-18 21:27:50 循环链表.数组解决约瑟夫环问题的比较 注意几点: 循环链表的建立不难,在删除循环链表中元素时,用pCur->next != pCur判断结束: 每一轮计数 ...
- COM, COM+ and .NET 的区别
所有的优秀程序员都会尽自己的最大努力去使自己所写的程序具有更好的可重用性,因为它可以让你快速地写出更加健壮和可升级性的程序. 有两种使代码重用的选择: 1.白盒:最简单的一种,就是把你的程序片拷贝到另 ...
- 不能设置sublime text 2 为默认编辑器
今天遇到一个有趣的事情,当我设置 css 样式表的默认打开方式的时候,却始终无法设置成功,系统总是随机选取一种打开方式来打开文件.比如:pdf.DW.txt等方式. 我设置默认打开方式的步骤如下: 1 ...
- Visual Studio Developer Command Prompt删除localdb的方法
PM> sqllocaldb.exe stop v11. LocalDB instance "v11.0" stopped. PM> sqllocaldb.exe de ...
- Web打印控件smsx.cab使用说明
在项目开发中,经常会用到页面打印的功能,在ASP.NET环境下推荐一款web打印控件smsx.cab. 使用方法:一般会先定义一个用于打印的母版页(Print.Master),在母版页上做好布局 ...
- 【转】Android中的颜色设置
原文网址:http://www.cnblogs.com/bluestorm/p/3644669.html 1.在android中经常看到设置的颜色为八位的十六进制的颜色值,例如: 1 2 3 publ ...
- create Context Menu in Windows Forms application using C# z
In this article let us see how to create Context Menu in Windows Forms application using C# Introduc ...
- HDU 3001 Travelling 3进制状压dp
题意:10个点,若干条边,边有花费,每个点最多走两次,求走过所有点,花费最少 分析:因为每个点最多走两次,所以联想到3进制,然后枚举状态,就行了(我也是照着网上大神的代码写的) #include &l ...