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. BZOJ 3295 【Cqoi2011】 动态逆序对

    Description 对于序列\(A\),它的逆序对数定义为满足\(i<j\),且\(A_i>A_j\)的数对\((i,j)\)的个数.给\(1\)到\(n\)的一个排列,按照某种顺序依 ...

  2. java调用.net asmx / wcf

    一.先用asmx与wcf写二个.net web service: 1.1 asmx web服务:asmx-service.asmx.cs using System; using System.Coll ...

  3. 韩国网页设计资料《网页设计大师2》JPG+PSD+TXT等 73.89G 百度云下载

    < 网页设计大师2 >超越第一代版本,提供更新更精美的网页素材模板.全部由国际顶级设计师精选打造,完全展示走在潮流 之前的设计风格.是网页设计师/UI交互界面设计师必备工具. < 网 ...

  4. [MetaHook] Surface hook

    Hook ISurface function. #include <metahook.h> #include <vgui/ISurface.h> using namespace ...

  5. 3DMax 物体选择方法

    全选: Ctrl + A, 取消选择:Ctrl +D 加选:ctrl+鼠标左键:减选:alt+鼠标 窗口与交叉:下面红框内的右边的按钮, 是切换两种模式: 选择模式一:只要选框碰到物体边缘, 就可选中 ...

  6. 高端大气上档次Ergotron Neo-Flex+MBP Retina的组合~

  7. mysql创建触发器

    触发器语句只有一句话 可以省略begin和end CREATE trigger `do_praise` after insert on praise for each row update post ...

  8. 0921MySQL 报错 ERROR 1290 (HY000): running with the --secure-file-priv

    http://blog.itpub.net/26506993/viewspace-2121850/ mysql> show variables like '%secure%';+-------- ...

  9. Hibernate的三种状态

     Hibernate的对象有3种状态,分别为:瞬时态(Transient). 持久态(Persistent).脱管态(Detached).处于持久态的对象也称为PO(Persistence Objec ...

  10. Android Retrofit网络请求Service,@Path、@Query、@QueryMap、@FieldMap (转)

    GET请求 多个参数在URL问号之后,且个数不确定 http://api.stay4it.com/News?newsId=1&type=类型1- http://api.stay4it.com/ ...