POJ-1655 Balancing Act
题目大意:一棵n个节点的树,找出最大子树最小的节点。
题目分析:过程类似求重心。
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<vector>
# include<queue>
# include<list>
# include<set>
# include<map>
# include<string>
# include<cmath>
# include<cstdlib>
# include<algorithm>
using namespace std;
# define LL long long const int N=1005;
const int INF=1000000000; int n,dp[N*20];
int size[N*20];
vector<int>e[N*20]; void init()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
e[i].clear();
int a,b;
for(int i=1;i<n;++i){
scanf("%d%d",&a,&b);
e[a].push_back(b);
e[b].push_back(a);
}
} void dfs(int u,int fa)
{
size[u]=1;
for(int i=0;i<e[u].size();++i){
int v=e[u][i];
if(v==fa) continue;
dfs(v,u);
size[u]+=size[v];
}
} void dfs1(int u,int fa)
{
dp[u]=n-size[u];
for(int i=0;i<e[u].size();++i){
int v=e[u][i];
if(v==fa) continue;
dp[u]=max(dp[u],size[v]);
dfs1(v,u);
}
} void solve()
{
dfs(1,-1);
dfs1(1,-1);
dp[0]=INF;
int root=0;
for(int i=1;i<=n;++i)
if(dp[i]<dp[root])
root=i;
printf("%d %d\n",root,dp[root]);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
solve();
}
return 0;
}
POJ-1655 Balancing Act的更多相关文章
- 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 树的重心
Balancing Act Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...
- 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组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同, ...
- POJ 1655 Balancing Act (求树的重心)
求树的重心,直接当模板吧.先看POJ题目就知道重心什么意思了... 重心:删除该节点后最大连通块的节点数目最小 #include<cstdio> #include<cstring&g ...
随机推荐
- iframe中的jquery ui modal dialog 覆盖父窗口
在iframe中 使用jquery ui dialog,弹出后可以覆盖父窗体 ///iframe中的jquery ui modal dialog 覆盖父窗口 function openDialog() ...
- 面向对象之prototype,__proto__
var person = function(name) { this.name = name }; person.prototype.getName = function() { return thi ...
- linux学习笔记3:linux的网络配置,rpm包,shell以及samba服务器的使用和安装
1.linux下的shell<linux命令.编辑器和shell编程> (1)shell种类有很多,常用的有三种,在linux可以通过ls -l /bin/*sh 来显示所有已安装的she ...
- 理解NSAttributedString
An NSAttributedString object manages character strings and associated sets of attributes (for exampl ...
- php大力力 [022节]php编程要有一种态度:渴望遇见麻烦
2015-08-27 php大力力022.php编程要有一种态度:渴望遇见麻烦 不能一遇到问题和麻烦,就烦躁焦躁. 写程序,写代码,调试实验就是天天遇见不可预期的错误bug,这是常态.老生常谈,要适应 ...
- 【Zhejiang University PATest】02-1. Reversing Linked List
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- ios oc ui 路径和颜色设置--崩溃解决方案
- (id)init{ self = [super init]; if (self) { _lineColor = CGColorCreateCopy([UIColor whiteColor].CGC ...
- Making the Grade_滚动数组&&dp
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- JLOI 斯迈利的赌注
直接高精度模拟,加上简单贪心 Program XJOI2263; ..] of longint; var a,b:arr; s1,s2:ansistring; i,j:longint; sum:int ...
- HDU 1300
http://acm.hdu.edu.cn/showproblem.php?pid=1300 这题大一就看到过,当时没读懂题目,今天再做就容易多了 题意:升序给出n个珍珠的的数量和价值,问买这些珍珠的 ...