poj1655 dfs
来源:互联网 发布:去广告软件adsafe 编辑:程序博客网 时间:2024/06/16 14:42
很简单的题目,找树的邻接点最大值的最小值,遍历一遍就可以了。。。
ACcode:
#include<cstdio>#include<cstring>const int ns=22222;int n,x,y,top,res,num;int v[ns],head[ns],to[ns<<1],next[ns<<1];void add(int p,int s){ next[top]=head[p]; to[top]=s; head[p]=top++;}int Max(int a1,int a2){ return a1>a2?a1:a2;}int Min(int a1,int a2){ return a1<a2?a1:a2;}int dfs(int rt){ v[rt]=0; int t,e,s=0,x=-1; for (int i=head[rt];i!=-1;i=next[i]) { e=to[i]; if (v[e]) { t=dfs(e); x=Max(x,t); s+=t; } } x=Max(x,n-1-s);// printf("rt=%d,num=%d,res=%d,x=%d\n",rt,num,res,x); if (num>x) { num=x,res=rt; } else if (num==x) { res=Min(res,rt); } return s+1;}int main(){ int T; scanf("%d",&T); while (T--) { scanf("%d",&n); top=res=0,num=1000000; memset(v,1,sizeof(v)); memset(head,-1,sizeof(head)); for (int i=1;i<n;i++) { scanf("%d%d",&x,&y); add(x,y); add(y,x); } dfs(1); printf("%d %d\n",res,num); } return 0;}
- poj1655 dfs
- poj1655(dfs) Balancing Act
- poj1655(Balancing Act + 树形dfs)
- POJ1655 Balancing Act——树的重心,Dfs
- POJ1655:Balancing Act(树的重心 DFS)
- POJ1655 【树的分治】
- poj1655树的重心
- POJ1655--Balancing Act
- poj1655树形dp
- poj1655(树的重心)
- 【poj1655】Balancing Act
- POJ1655 Balancing Act
- [POJ1655] Balancing Act
- POJ1655 Balancing Act(树状DP)
- poj1655 2378 3107 树形dp
- poj1655 Balancing Act 树状dp
- POJ1655:Balancing Act(树形DP)
- POJ1655——Balancing Act
- Spring mvc Interceptor 解决Session超时配置流程
- activemq示例
- ZOJ2060
- Appcode使用技巧
- winsock的部分总结
- poj1655 dfs
- Android 4.0日历(calendar)源码分析之概览
- 带您深入了解MySQL ifnull()函数
- NOJ-(strling数)
- 鼠标放在图层或图片上面,可导致图层或图片抖动的JS特效代码
- (转)不要一辈子靠技术生存
- c语言内存管理例题
- curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo
- (W3C学习笔记) HTML 演变时间线