Balancing Act(树的重心)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 14070 | Accepted: 5939 | 
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<cstring>
using namespace std;
int n,x,y,G,sumedge,t;
int head[],size[],dad[],dp[];
struct Edge
{
int x,y,nxt;
Edge(int x=,int y=,int nxt=):x(x),y(y),nxt(nxt){}
}edge[];
void add(int x,int y)
{
edge[++sumedge]=Edge(x,y,head[x]);
head[x]=sumedge;
}
void init()
{
sumedge=;
memset(head,,sizeof(head));
memset(size,,sizeof(size));
memset(dad,,sizeof(dad));
memset(dp,,sizeof(dp));
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
}
void dfs(int x)
{
size[x]=;
for(int i=head[x];i;i=edge[i].nxt)
{
int v=edge[i].y;
if(dad[x]!=v)
{
dad[v]=x;
dfs(v);
size[x]+=size[v];
dp[x]=max(dp[x],size[v]);//最大的孩子
}
}
dp[x]=max(dp[x],n-size[x]);//不是子树的那一堆
}
void print()
{
int ans=0x7fffff;
for(int i=;i<=n;i++)
if(dp[i]<ans)ans=dp[i],G=i;
printf("%d %d\n",G,ans);
}
int main()
{
scanf("%d",&t);
while(t--)
{
init();
dfs();
print();
}
return ;
}
Balancing Act(树的重心)的更多相关文章
- POJ 1655 Balancing Act 树的重心
		Balancing Act Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ... 
- 『Balancing Act 树的重心』
		树的重心 我们先来认识一下树的重心. 树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 根据树的重心的定义,我们可 ... 
- POJ1655 Balancing Act(树的重心)
		题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. #include <bits/stdc++.h> using namespace std; #define REP ... 
- poj-1655 Balancing Act(树的重心+树形dp)
		题目链接: Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11845 Accepted: 4 ... 
- PKU 1655 Balancing Act(树+树的重心)
		#include<cstdio> #include<cstring> #include<algorithm> #define maxn 20005 using na ... 
- POJ 1655 - Balancing Act 树型DP
		这题和POJ 3107 - Godfather异曲同工...http://blog.csdn.net/kk303/article/details/9387251 Program: #include&l ... 
- poj1655 Balancing Act 找树的重心
		http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submis ... 
- POJ 1655 Balancing Act【树的重心】
		Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14251 Accepted: 6027 De ... 
- POJ 1655.Balancing Act 树形dp 树的重心
		Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14550 Accepted: 6173 De ... 
- POJ.1655 Balancing Act   POJ.3107 Godfather(树的重心)
		关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ... 
随机推荐
- Systemtap examples, Network - 4 Monitoring TCP Packets
			http://blog.163.com/digoal@126/blog/static/16387704020131014104256627/ 例子来自tcpdumplike.stp脚本, 当tcp ... 
- ng-options  bug解决方案(示例)
			情况: 无法获取 ng-model 的值 解决方案: 绑定到对象的属性值上 1.页面 <ion-view hide-nav-bar="true"> <ion-co ... 
- weex  项目开发(一) weex create project  与  weex init project  的区别
			开发环境配置:http://www.cnblogs.com/crazycode2/p/7822961.html 1. weex create project 与 weex init project ... 
- weex  引导页(guide)页面
			slider 和 indicator 都是 weex 的内置组件,且 indicator 是 slider 的子组件. 1.报错处理 原因解析:indicator 样式页面渲染慢 解决方案:indic ... 
- 使用maven创建项目和cannot change version web module 3.0
			近期下载了最新的Eclipse mars.2, 这个eclipse自带了maven插件,于是就用maven尝试创建一个java web项目. 第一步,例如以下图所看到的选择 Maven Project ... 
- java开始到熟悉70-71
			本次内容:file类 package array; /** * file类 */ import java.io.File; import java.io.IOException; public cla ... 
- PHP读取excel(6)
			有时候我们只需要读取某些指定sheet,具体代码如下: <?php header("Content-Type:text/html;charset=utf-8"); //引入读 ... 
- openwrt procd 运行的一些log
			void procd_inittab(void) { #define LINE_LEN 128 FILE *fp = fopen(tab, "r"); struct init_ac ... 
- 从数据源拉取数据,将数据内容与一组搜索项做比对  go func()  chanel
			https://github.com/goinaction/code [root@hadoop3 sample]# go run main.go 2018/07/30 17:45:39 Registe ... 
- Axure Base 01
			名词解释: 线框图:一般就是指产品原型,比如:把线框图尽快画出来和把原型尽快做出来是一个意思. axure元件:也叫axure组件或axure部件,系统自带了一部分最基础常用的,网上也有很多别人做好的 ... 
