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 ...
随机推荐
- Spring MVC集成Spring Data Reids和Spring Session实现Session共享
说明:Spring MVC中集成Spring Data Redis和Spring Session时版本是一个坑点,比如最新版本的Spring Data Redis已经不包含Jedis了,需要自行引入. ...
- Linux下使用curl进行http请求(转)
curl在Linux下默认已经安装,Windows需要自行安装. 下载地址:https://curl.haxx.se/download.html Windows离线版本:链接:http://pan.b ...
- Java屏幕截图工具 捕获屏幕
原文:http://www.open-open.com/code/view/1420037709781 import java.awt.BorderLayout; import java.awt.Co ...
- 【flyway】Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' def
报错如下: "2018-03-20 12:58:09.585 WARN 18026 — [ restartedMain] ConfigServletWebServerApplicationC ...
- 【java】java base64编码与解码
参考地址:http://blog.csdn.net/zhou_kapenter/article/details/62890262 要求:JDK1.8+ 使用java原生工具类即可实现 [这里展示字符串 ...
- linux驱动开发流程
嵌入式linux驱动开发流程嵌入式系统中,操作系统是通过各种驱动程序来驾驭硬件设备的.设备驱动程序是操作系统内核和硬件设备之间的接口,它为应用程序屏蔽了硬件的细节,这样在应用程序看来,硬件设备只是一个 ...
- k-svd字典学习,稀疏编码
1. K-SVD usage: Design/Learn a dictionary adaptively to betterfit the model and achieve sparse s ...
- Spring官方文档翻译——15.1 介绍Spring Web MVC框架
Part V. The Web 文档的这一部分介绍了Spring框架对展现层的支持(尤其是基于web的展现层) Spring拥有自己的web框架--Spring Web MVC.在前两章中会有介绍. ...
- STL review:vector & string & map & struct
I.vector 1.头文件:#include<vector> //容器vector是一个能实现随机存取.插入删除的动态数组,还可以当栈使. ...
- 在vc6.0下编的对话框界面如果没做过其他处理,往往显的很生硬,怎么样才能使他有Windows XP的风格呢,其实也很简单,我们来看看下面两种方法。
在vc6.0下编的对话框界面如果没做过其他处理,往往显的很生硬,怎么样才能使他有Windows XP的风格呢,其实也很简单,我们来看看下面两种方法. 方法一: 1.首先确认你在Windows ...