POJ-1655 Balancing Act(树的重心)
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 题意就是给你一棵无根树,让你找到一个点,去掉这个点之后使得剩下的子树的最大节点数最小; 思路,就是求树的重心,下面先给出树的重心的定义:对于一棵n个节点的无根树,找到一个点使得把树变成一棵以该节点为根的有根树,这时的最大子树的节点数最小。
定义sizes[i]表示i的最大子树的节点数,定义dp[i]为以i为根的最大子树的节点数。然后递归求解。
#include <iostream>
#include <cstdio>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=1e6+; int dp[maxn];
int sizes[maxn];
int ans,n,sum;
vector<int> v[maxn];//二维矩阵存图
void dfs(int x,int fa)
{
sizes[x]=;
int maxx=;
for(int i=;i<v[x].size();i++)
{
int y=v[x][i];
if(y!=fa)
{
dfs(y,x);
sizes[x]+=sizes[y];
maxx=max(maxx,sizes[y]);
} }
dp[x]=max(maxx,n-sizes[x]);
if(sum>dp[x])
{
ans=x;
sum=dp[x];
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
sum=0x3f3f3f3f;
scanf("%d",&n);
memset(dp,,sizeof(dp));
memset(sizes,,sizeof(sizes));
for(int i=;i<=n;i++)v[i].clear();
for(int i=;i<n;i++)
{
int L,K;
scanf("%d%d",&L,&K);
v[K].push_back(L);
v[L].push_back(K);
}
dfs(,-);
printf("%d %d\n",ans,dp[ans]);
} return ;
}
POJ-1655 Balancing Act(树的重心)的更多相关文章
- POJ 1655 Balancing Act 树的重心
Balancing Act Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...
- POJ 1655 - Balancing Act 树型DP
这题和POJ 3107 - Godfather异曲同工...http://blog.csdn.net/kk303/article/details/9387251 Program: #include&l ...
- 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 求树的重心【树形dp】
poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...
- 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(找树的重心)
Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或 ...
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
- POJ 1655 - Balancing Act - [DFS][树的重心]
链接:http://poj.org/problem?id=1655 Time Limit: 1000MS Memory Limit: 65536K Description Consider a tre ...
- POJ 1655 Balancing Act【树的重心模板题】
传送门:http://poj.org/problem?id=1655 题意:有T组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同, ...
随机推荐
- 云上kafka和自建kafka对比
说起Kafka,许多使用者对它是又爱又恨.Kafka是一种分布式的.基于发布/订阅的消息系统,其极致体验让人欲罢不能,但操心的运维.复杂的安全策略.可靠性易用性的缺失.算不上极致的性能发挥.并不丰富的 ...
- hdu 1799 (循环多少次?)(排列组合公式)
循环多少次? Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 自己定义 View 基础和原理
课程背景: 在 Android 提供的系统控件不能满足需求的情况下,往往须要自己开发自己定义 View 来满足需求,可是该怎样下手呢.本课程将带你进入自己定义 View 的开发过程,来了解它的一些原理 ...
- 【树剖求LCA】树剖知识点
不太优美但是有注释的版本: #include<cstdio> #include<iostream> using namespace std; struct edge{ int ...
- C++之内部类(内部类就是外部类的友元类,单向友元。只是内部类比友元类多了一点权限)
1. 内部类的概念 如果一个类定义在另一个类的内部,这个内部类就叫做内部类.注意此时这个内部类是一个独立的类,它不属于外部类,更不能通过外部类的对象去调用内部类.外部类对内部类没有任何优越的访问权限. ...
- (函数即服务)Faas的现状与未来
刚看到jolestar一位从法律转行程序员的前辈写了一篇Faas现状与未来的文章,里面很多观点都很有启发,或许正如他说的那样,由于Faas能较好的解决资源利用率和开发效率问题,2018年Faas将变得 ...
- 要自己当技术使用astgo运营网络电话系统,必须掌握的基本技术
知道什么是centos 知道怎么远程访问centos服务器 (常用工具 Secure Shell Client.WINSCP) 知道重启服务器的命令是 reboot 知道你的服务器是没有图形界面的,所 ...
- 湖南集训day8
难度:☆☆☆☆☆☆☆ /* 可以先考虑一维,可知 模k意义下相同的前缀和任意两个相减都是k的倍数 问题等价于统计前缀何种模k相同的数的对数. 多维的时候二维前缀和,压行或者压列,n^3可以解决. */ ...
- String,StringBuffer和StringBuilder
在执行速度方面的比较:StringBuilder > StringBuffer StringBuffer与StringBuilder,他们是字符串变量,是可改变的对象,每当我们用它们对字符串做操 ...
- 【HTML5】基于HTML5的高性能动画与游戏
其实这篇文章类似版本早在12年就在网上各处出现了,也随着HTML5的兴起,HTML的新特性也是倍受开发者们追捧,自然相关HTML5的高性能动画与游戏的相关文章也是层出不穷的,笔者也是在12年接触的相关 ...