poj 1655 树的重心
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 13178 | Accepted: 5565 |
Description
For example, consider the tree:

Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node 1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees has two nodes, so the balance of node 1 is two.
For each input tree, calculate the node that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number.
Input
Output
Sample Input
1
7
2 6
1 2
1 4
4 5
3 7
3 1
Sample Output
1 2
Source
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define dazhi 2147483647
#define bug() printf("!!!!!!!")
#define M 200005
using namespace std;
struct node
{
int from;
int to;
} N[*M];
int n;
int pre[M];
int nedge=;
int son[M];
int vis[M];
int l;
int r;
int ans;
int re;
int t;
void add(int f,int t)
{
nedge++;
N[nedge].to=t;
N[nedge].from=pre[f];
pre[f]=nedge;
}
int getnode(int root)
{
vis[root]=;
son[root]=;
int temp=;
for(int i=pre[root];i;i=N[i].from)
{
int x=N[i].to;
if(vis[x]==)
{
getnode(x);
son[root]+=son[x]+;
temp=max(temp,son[x]+);
}
}
temp=max(temp,n-son[root]-);
if(temp<ans||(temp==ans&&root<re))
{
ans=temp;
re=root;
}
}
int main()
{
scanf("%d",&t);
for(int j=; j<=t; j++)
{
memset(pre,,sizeof(pre));
memset(N,,sizeof(N));
memset(vis,,sizeof(vis));
nedge=;
re=M;
ans=M;
scanf("%d",&n);
for(int i=; i<=n-; i++)
{
scanf("%d %d",&l,&r);
add(l,r);
add(r,l);
}
getnode();
printf("%d %d\n",re,ans);
}
return ;
}
poj 1655 树的重心的更多相关文章
- poj 1655 树的重心 && define注意事项
http://blog.csdn.net/acdreamers/article/details/16905653 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果 ...
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
- POJ 1655 求树的重心
POJ 1655 [题目链接]POJ 1655 [题目类型]求树的重心 &题意: 定义平衡数为去掉一个点其最大子树的结点个数,求给定树的最小平衡数和对应要删的点.其实就是求树的重心,找到一个点 ...
- POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...
- POJ 1655 - Balancing Act - [DFS][树的重心]
链接:http://poj.org/problem?id=1655 Time Limit: 1000MS Memory Limit: 65536K Description Consider a tre ...
- POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)
参考网址:http://blog.csdn.net/acdreamers/article/details/16905653 树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的 ...
- poj 1655 Balancing Act(找树的重心)
Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或 ...
- poj 1655 Balancing Act 求树的重心【树形dp】
poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...
- POJ 1655 Balancing Act【树的重心模板题】
传送门:http://poj.org/problem?id=1655 题意:有T组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同, ...
随机推荐
- MYSQL存储过程调试过程
mysql不像oracle有plsqldevelper工具用来调试存储过程,所以有几种简单的方式追踪执行过程: 1.用一张临时表,记录调试过程: 2.直接在存储过程中,增加select xxx,在控 ...
- python基础知识-8-三元和一行代码(推导式)
python其他知识目录 1.三元运算(三目运算) 三元运算符就是在赋值变量的时候,可以直接加判断,然后赋值格式:[on_true] if [expression] else [on_false]re ...
- hbase 修复 hbck
hbase 修复使用hbck 新版本的 hbck 可以修复各种错误,修复选项是: (1)-fix,向下兼容用,被-fixAssignments替代 (2)-fixAssignments,用于修复reg ...
- [C++] Solve "Launch Failed. Binary not found." error on Eclipse
This error is that the default lanch configuration is not being created for this project. To solve i ...
- c# apache服务器请求得到数据(初级)
1.代码: string data = new WebClient().DownloadString("http://localhost:81/123.txt");
- OpenCV学习笔记——Mat类型数据存储
CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number] 比如 CV_8UC3 表示 ...
- Java 常用类String类、StringBuffer类
常用类 String类.StringBuffer类 String代表不可变的字符序列 "xxxxxxx"为该类的对象 举例(1) public class Test { publi ...
- 补发9.26“天天向上”团队Scrum站立会议
组长:王森 组员:张金生 张政 栾骄阳 时间:2016.09.26 地点:612寝 组员 已完成 未完成 王森 可行性分析 找出设计亮点 张金生 寻找UI素材 设计用户操作 张政 搭建环境 基础逻辑框 ...
- 修改Oracle redo.log文件的大小
1.查看当前日志组成员: SQL> select member from v$logfile; MEMBER ------------------------------------------ ...
- linux下c/c++的文件操作
opendir,readdir,closedir, stat()查询文件状态 open(), O_TRUNC这个Flag会把打开的文件清零... 文件锁:fcntl, F_GETLK , F_SETL ...