http://poj.org/problem?id=1655 (题目链接)

题意

  给出一棵树,求树上一点使得使得删除一点后该树的最大子树最小。

solution

  树的重心裸题。

  随意取一点作为根节点,dfs维护当前节点的最大子树大小以及它的父亲子树大小,更新答案。

代码

// poj1655
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<ctime>
#define LL long long
#define inf 2147483640
#define eps 1e-10
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=20010;
struct edge {int to,next;}e[maxn<<2];
int head[maxn],vis[maxn],son[maxn],cnt,size,ans,n; void link(int u,int v) {
e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;
e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;
}
void dfs(int x) {
vis[x]=1,son[x]=0;
int tmp=0;
for (int i=head[x];i;i=e[i].next) if (!vis[e[i].to]) {
dfs(e[i].to);
son[x]+=son[e[i].to]+1;
tmp=max(tmp,son[e[i].to]+1);
}
tmp=max(tmp,n-son[x]-1);
if (tmp<size || (tmp==size && x<ans)) size=tmp,ans=x;
}
int main() {
int T;scanf("%d",&T);
while (T--) {
scanf("%d",&n);
memset(vis,0,sizeof(vis));memset(head,0,sizeof(head));
cnt=ans=0;size=inf;
for (int u,v,i=1;i<n;i++) {
scanf("%d%d",&u,&v);
link(u,v);
}
dfs(1);
printf("%d %d\n",ans,size);
}
return 0;
}

  

  

【poj1655】Balancing Act的更多相关文章

  1. 【POJ 1655】 Balancing Act

    [题目链接] 点击打开链接 [算法] 树形DP求树的重心 [代码] #include <algorithm> #include <bitset> #include <cc ...

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

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

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

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

  4. poj1655 Balancing Act 找树的重心

    http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  5. POJ1655 Balancing Act(树的重心)

    题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. #include <bits/stdc++.h> using namespace std; #define REP ...

  6. poj-1655 Balancing Act(树的重心+树形dp)

    题目链接: Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11845   Accepted: 4 ...

  7. poj1655 Balancing Act (dp? dfs?)

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14247   Accepted: 6026 De ...

  8. Balancing Act(poj1655)

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12703   Accepted: 5403 De ...

  9. 【转载】史上最全:TensorFlow 好玩的技术、应用和你不知道的黑科技

    [导读]TensorFlow 在 2015 年年底一出现就受到了极大的关注,经过一年多的发展,已经成为了在机器学习.深度学习项目中最受欢迎的框架之一.自发布以来,TensorFlow 不断在完善并增加 ...

随机推荐

  1. SQL Server 2005、2008 的 datetime 值范围(转)

    SQL Server 2005.2008 的 datetime 最小值是:1753-01-01 00:00:00 最大值是:9999-12-31 23:59:59.997 这与 .NET 中的 Dat ...

  2. 添加Distributor失败

    上周做了一个case,客户无法为SQL Server instance配置remote distributor. 下面分享一下排查问题的过程,希望对您排查类似的问题所有帮助. 客户的环境中的SQL S ...

  3. Generate Parentheses

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  4. MVC3.0 上传图片并生成缩略图

    转自:http://mikelai.blog.163.com/blog/static/18411126620118771732675/ Controller: public ActionResult ...

  5. struts2使用Convention Plugin在weblogic上以war包部署时,找不到Action的解决办法

    环境: struts 2.3.16.3 + Convention Plugin 2.3.16.3 实现零配置 现象:以文件夹方式部署在weblogic(10.3.3)上时一切正常,换成war包部署,运 ...

  6. 正式版/免费版 Xamarin 体验与拥抱

    感谢MS, 感谢老纳.终于把 Xamarin 这个磨人的小妖精给收了,在也不用向大神要破解补丁了, 终于可以光明正大的使用了!! 跟据实践, 如果你们想体验一下 .NET 开发 IOS /Androi ...

  7. 由一次程序崩溃引起的对new表达式的再次学习

    1. 起因 某天,一个同事跟我反馈说在windows上调试公司产品的一个交易核心时出现了使用未初始化的指针导致后台服务崩溃的情况.示例代码如下所示: struct sample { ][]; //.. ...

  8. vi实战记录

    vi编辑器在Unix和Linux中比较早期的,Vim是vi的扩展集,是对vi的加强. 服务器最小化,默认集成vi编辑器!了解vi常用命令,工作起来颇有-洪荒之力!!! 01.关于退出 :wq!  -- ...

  9. Vware Workstation pro 12|虚拟机

    Vmware是比较不错的PC虚拟化软件,vmware11+不在支持32的系统安装!体积比之前小了很多 VMware 12 官方中文页面 http://vmware.com/cn/products/wo ...

  10. C# 压缩与解压字符串(面试题)

    /* * 题目:压缩字符串.如“abbcccddddeef”,压缩成“a1b2c3d4e2f1” * 解题: 这个题目也是面试常见的题目.看似很简单,其实暗藏杀机.一般的想法就是,一边遍历,一边计数, ...