HIHO #1050 : 树中的最长路 (dfs 树的直径)
来源:互联网 发布:stm8单片机gpio引脚 编辑:程序博客网 时间:2024/05/17 06:22
题目链接
树的直径的计算:任选一点dfs到最远点p,然后从p在dfs到最远点的距离就是树的直径
#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>#include<string>#include<cmath>#include<queue>#include<map>#include<set>#include<cstdlib>#include<vector>using namespace std;#define cl(a,b) memset(a,b,sizeof(a))#define LL long long#define pb push_back#define gcd __gcd#define For(i,j,k) for(int i=(j);i<k;i++)#define lowbit(i) (i&(-i))#define _(x) printf("%d\n",x)const int maxn = 3e6+10;const int inf = 1 << 28;int n;vector<int> G[maxn];int tmp;int sum;void dfs(int u,int fa,int dis){ if(sum<dis){ sum = dis; tmp = u; } for(int i=0;i<G[u].size();i++){ int v = G[u][i]; if(v==fa)continue; dfs(v,u,dis+1); }}int main(){ scanf("%d",&n); for(int i=0;i<n-1;i++){ int x,y;scanf("%d%d",&x,&y); G[x].pb(y);G[y].pb(x); } sum=0; dfs(1,-1,0);//cout<<"tmp "<<tmp<<" sum "<<sum<<endl; sum = 0; dfs(tmp,-1,0); printf("%d\n",sum); return 0;}
0 0
- HIHO #1050 : 树中的最长路 (dfs 树的直径)
- Hiho #1050 : 树中的最长路 【树的直径】
- 树的直径(图中最长路)——hiho 1050
- hiho一下#1050 : 树中的最长路
- hiho#1050 : 树中的最长路
- hiho#1050 : 树中的最长路
- [HIHO] 1050 树中的最长路
- 树的直径(最长路)问题
- 树的直径(最长路)
- 树的最长直径
- 树的直径最长路证明
- 求树的直径(最长路径)
- 树的直径(最长路) 的详细证明
- 树的直径 (树上的最长路)
- 树的直径(最长路) 的详细证明
- 树的直径(最长路) 的详细证明
- Cow Marathon(树的直径)(最长路)
- hdu2196 Computer(树的直径||树中的最长路径)
- 安卓学习笔记---如何查看android应用签名文件的信息
- leetcode 343. Integer Break ->可以证明,拆成的3越多,最后的乘积越大
- 如何在Myeclipse下安装activiti插件包
- mysql中创建带小数点的数据库名、表名、列名
- VS2013无法创建项目,提示“无法提取项目文件 *.vcxproj”,请确认<Import>声明中的路径正确,且磁盘上存在该文件
- HIHO #1050 : 树中的最长路 (dfs 树的直径)
- banner设计技巧分享
- Java的文件读写操作
- 【HDU1536】S-Nim(博弈)
- 杭电acm2090--算菜价
- jQuery函数attr()和prop()的区别
- getFragmentManager和getSupportFragmentManager的区别
- OGL点参数圆形衰减/贴花/多边形偏移技术
- case when decode