博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bzoj3391[Usaco2004 Dec]Tree Cutting网络破坏*
阅读量:7024 次
发布时间:2019-06-28

本文共 1216 字,大约阅读时间需要 4 分钟。

题意:

给一棵树,问去掉哪个点后可以使剩下的每个子树大小都小于等于节点总数的一半。n≤10000。

题解:

dfs的时候求一下子树大小,以当前节点的父节点为根节点的子树大小为n-以当前节点为根节点的子树大小。

代码:

1 #include 
2 #include
3 #include
4 #include
5 #define inc(i,j,k) for(int i=j;i<=k;i++) 6 #define maxn 10010 7 using namespace std; 8 9 inline int read(){10 char ch=getchar(); int f=1,x=0;11 while(ch<'0'||ch>'9'){
if(ch=='-')f=-1; ch=getchar();}12 while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();13 return f*x;14 }15 struct e{
int t,n;}es[maxn*2]; int ess,g[maxn];16 void pe(int f,int t){17 es[++ess]=(e){t,g[f]}; g[f]=ess; es[++ess]=(e){f,g[t]}; g[t]=ess;18 }19 int sz[maxn],n,ans[maxn],tot;20 void dfs(int x,int fa){21 int mx=0; sz[x]=1; for(int i=g[x];i;i=es[i].n)if(es[i].t!=fa){22 dfs(es[i].t,x); sz[x]+=sz[es[i].t]; mx=max(mx,sz[es[i].t]);23 }24 mx=max(mx,n-sz[x]); if(mx<=n/2)ans[++tot]=x;25 }26 int main(){27 n=read(); inc(i,1,n-1){
int x=read(),y=read(); pe(x,y);} dfs(1,0);28 if(!tot){printf("NONE"); return 0;}29 sort(ans+1,ans+1+tot); inc(i,1,tot)printf("%d\n",ans[i]); return 0;30 }

 

20161114

转载于:https://www.cnblogs.com/YuanZiming/p/6067148.html

你可能感兴趣的文章
YAML 语法 规则
查看>>
css @语法,@规则 @import @charset @font-face @fontdef @media @page
查看>>
asp.net系统过滤器、自定义过滤器
查看>>
CSS3 Animation
查看>>
window 下常用的一些命令和应用
查看>>
mysql having的用法
查看>>
重新认识java-忽视的注释
查看>>
Sierpinski三角
查看>>
Dos下查看端口
查看>>
深入探讨Java类加载器
查看>>
Go 开发 HTTP
查看>>
textview的滚动
查看>>
使用JQuery.validate后的bootstrap风格校验提示‏
查看>>
iOS开发中NSTimer的开启与关闭
查看>>
NotePad++中SourceCookifier插件的使用
查看>>
jvm gc日志分析
查看>>
springmvc hello-servlet.xml配置文件
查看>>
kindeditor + syntaxhighlighter 使文章内的插入代码高亮显示
查看>>
基于微博数据用 Python 打造一颗“心”
查看>>
我的Linux发行版/桌面环境选择之路
查看>>