题目大意:一棵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的更多相关文章

  1. POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)

    关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...

  2. poj 1655 Balancing Act 求树的重心【树形dp】

    poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...

  3. POJ 1655 Balancing Act【树的重心】

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14251   Accepted: 6027 De ...

  4. POJ 1655.Balancing Act 树形dp 树的重心

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14550   Accepted: 6173 De ...

  5. poj 1655 Balancing Act(找树的重心)

    Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或 ...

  6. POJ 1655 Balancing Act 树的重心

    Balancing Act   Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...

  7. POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

  8. POJ 1655 - Balancing Act - [DFS][树的重心]

    链接:http://poj.org/problem?id=1655 Time Limit: 1000MS Memory Limit: 65536K Description Consider a tre ...

  9. POJ 1655 Balancing Act【树的重心模板题】

    传送门:http://poj.org/problem?id=1655 题意:有T组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同, ...

  10. POJ 1655 Balancing Act (求树的重心)

    求树的重心,直接当模板吧.先看POJ题目就知道重心什么意思了... 重心:删除该节点后最大连通块的节点数目最小 #include<cstdio> #include<cstring&g ...

随机推荐

  1. ctime、atime

    Linux系统文件有三个主要的时间属性,分别是ctime(change time, 而不是create time), atime(access time), mtime(modify time).后来 ...

  2. Fix the Can’t clobber writable file error in Perforce Version Control System - forward

    http://easyprograming.com/eclipse-articles/57-fix-the-cant-clobber-writable-file-error-in-perforce-v ...

  3. win10系统的点评

    Windows 10 是美国微软公司所研发的新一代跨平台及设备应用的操作系统.在正式版本发布一年内,所有符合条件的Windows7.Windows 8.1的用户都将可以免费升级到Windows 10, ...

  4. self.view 的不当操作造成死循环

    如题,在创建ContentView的时候,例子如下 NSString *viewClassName = NSStringFromClass([self class]); viewClassName = ...

  5. ALAssets的两种用法

    一: ALAssetsGroupEnumerationResultsBlock resultsBlock = ^(ALAsset *result, NSUInteger index, BOOL *st ...

  6. Note_Master-Detail Application(iOS template)_01_YJYAppDelegate.h

    //YJYAppDelegate.h #import <UIKit/UIKit.h> @interface YJYAppDelegate : UIResponder <UIAppli ...

  7. RFID Hacking①:突破门禁潜入FreeBuf大本营

    某天,偶然间拿到了FreeBuf Pnig0s同学的工卡信息,终于有机会去做一些羞羞的事情了 引子 以下故事纯属虚构,如有雷同,纯属巧合. 我应聘了一个大型IT公司的"网络攻击研究部经理&q ...

  8. python简介-copy

    首先python的老家https://www.python.org/ 原文http://www.runoob.com/python/python-intro.html Python 简介 Python ...

  9. iOS log第三方库之间的对比

  10. Android沉浸式(侵入式)标题栏(状态栏)Status(一)

     Android沉浸式(侵入式)标题栏(状态栏)Status(一) 现在越来越多的APP设计采用这种称之为沉浸式状态栏(Status)的设计,这种沉浸式状态栏又称之"侵入式"状 ...