【poj1655】Balancing Act
来源:互联网 发布:电火花电脉冲编程 编辑:程序博客网 时间:2024/06/07 06:20
http://poj.org/problem?id=1655 (题目链接)
题意:给出一棵树,求树上一点使得使得删除一点后该树的最大子树最小。
solution
树的重心裸题。
随意取一点作为根节点,dfs维护当前节点的最大子树大小以及它的父亲子树大小,更新答案。
代码:
// poj1655#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<cmath>#define LL long long#define inf 2147483640#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 insert(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 i=1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); insert(u,v); } dfs(1); printf("%d %d\n",ans,size); } return 0;}
0 0
- poj1655(dfs) Balancing Act
- POJ1655--Balancing Act
- 【poj1655】Balancing Act
- POJ1655 Balancing Act
- [POJ1655] Balancing Act
- POJ1655 Balancing Act(树状DP)
- poj1655(Balancing Act + 树形dfs)
- poj1655 Balancing Act 树状dp
- POJ1655:Balancing Act(树形DP)
- POJ1655——Balancing Act
- poj1655 Balancing Act(树形dp)
- 树重心 poj1655 Balancing Act
- poj1655 Balancing Act 【树形DP(很弱)】
- POJ1655 Balancing Act (树的重心)
- POJ1655 Balancing Act 求树的重心
- 【poj1655】Balancing Act 求树的重心
- poj1655 Balancing Act(树形dp)
- 【POJ1655】Balancing Act【树的重心】
- HDU 2062 Subset sequence[排列组合]
- HttpModule实现URL重写
- Java中的继承和多态
- MSHFlexGrid控件——点击某行时该行被选中
- nginx监听端口非80时的转发问题的配置文件
- 【poj1655】Balancing Act
- 杨氏矩阵
- linux上安装,运行tomcat
- tyvj p1063 数字串 贪心
- Java中的接口(interface)
- IP地址、子网掩码、网络号、主机号、网络地址、主机地址
- 什么是单例模式?
- 官方URLRewriter Module实现URL重写
- 我上传的CSDN资源无法使用的处理方法