直径定义:树上的最长路径,不妨设端点分别为s,t

可以证明(感觉):每个点到其最远点必定为s or t,反之亦然

首先,第一次dfs找到s

然后,第二次dfs以s为根找到t

最后,第三次dfs以t为根

比较二三两次的当前点深度可得到答案

 #include <set>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=,MAXM=;
struct edge{
int u,v,w,next;
edge(int u=,int v=,int w=,int next=):u(u),v(v),w(w),next(next){}
}; edge E[MAXM];
int head[MAXN],fa[MAXN],dis[MAXN],ans[MAXN],cnt,n,s,t,maxdis;
void add(int u,int v,int w){
E[++cnt]=edge(u,v,w,head[u]),head[u]=cnt;
E[++cnt]=edge(v,u,w,head[v]),head[v]=cnt;
} int dfs(int x,int fa){
for(int i=head[x];i;i=E[i].next)
if(E[i].v!=fa){
dis[E[i].v]=max(dis[x]+E[i].w,dis[E[i].v]);
dfs(E[i].v,x);
}
} int search(int root){
int x=root;
maxdis=,memset(dis,,sizeof(dis)),dfs(root,root);
for(int i=;i<=n;i++)if(dis[i]>maxdis)maxdis=dis[i],x=i;
return x;
} int main(){
while(scanf("%d",&n)!=EOF){
cnt=;
memset(head,,sizeof(head));
for(int u=,v,w;u<=n;u++)scanf("%d%d",&v,&w),add(u,v,w);
int s=search();
int t=search(s);
for(int i=;i<=n;i++)ans[i]=dis[i];
search(t);
for(int i=;i<=n;i++)printf("%d\n",max(ans[i],dis[i]));
}
return ;
}

C++-HDU2196-Computer-[树的直径]的更多相关文章

  1. [hdu2196]Computer树的直径

    题意:求树中距离每个节点的最大距离. 解题关键:两次dfs,第一次从下向上dp求出每个节点子树中距离其的最大距离和不在经过最大距离上的子节点上的次大距离(后序遍历),第二次从上而下dp求出其从父节点过 ...

  2. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  3. hdu 2196 Computer 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  4. HDOJ 2196 Computer 树的直径

    由树的直径定义可得,树上随意一点到树的直径上的两个端点之中的一个的距离是最长的... 三遍BFS求树的直径并预处理距离....... Computer Time Limit: 1000/1000 MS ...

  5. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  6. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  7. HDU 2196.Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. [J]computer network tarjan边双联通分量+树的直径

    https://odzkskevi.qnssl.com/b660f16d70db1969261cd8b11235ec99?v=1537580031 [2012-2013 ACM Central Reg ...

  9. computer(树形dp || 树的直径)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  10. hdu2196 树的直径 + bfs

    //Accepted 740 KB 15 ms //树的直径 //距离一个顶点最远的点一定是树的直径的一个端点 #include <cstdio> #include <cstring ...

随机推荐

  1. mysql修改密码、创建用户、开放远程

    创建密码: MariaDB [(none)]> use mysql; MariaDB [mysql]> UPDATE user SET password=password('newpass ...

  2. 递归查询 start with connect by prior

    1.语法:start with 子节点ID='...' connect by prior 子节点ID = 父节点ID 含义:查询结果我所有的后代节点(包括我) 例子: select id,parent ...

  3. so the first day

    welcome to the world it sucks but you gona love it

  4. 曼孚科技:AI算法领域常用的39个术语(上)

    ​算法是人工智能(AI)核心领域之一. 本文整理了算法领域常用的39个术语,希望可以帮助大家更好地理解这门学科. 1. Attention 机制 Attention的本质是从关注全部到关注重点.将有限 ...

  5. JavaScript - request封装

    request封装--微信小程序使用async,await ES5 参考代码 var request = function(param){ var _this = this; $.ajax({ typ ...

  6. 关于JavaScript的原型继承与原型链

    在讨论原型继承之前,先回顾一下关于创建自定义类型的方式,这里推荐将构造函数和原型模式组合使用,通过构造函数来定义实例自己的属性,再通过原型来定义公共的方法和属性. 这样一来,每个实例都有自己的实例属性 ...

  7. linux常见目录介绍

    /bin:/usr/bin: 可执行二进制文件目录,如常用命令ls.cat /boot: 放置linux启动时用到的一些文件,建议分区的时候独立分区 /dev: 存在linux系统下的设备文件,访问该 ...

  8. java下的slf4j

    一.导读 我们使用log4j框架时,经常会用slf4j-api.在运行时,经常会遇到如下的错误提示: ? 1 2 3 4 5 SLF4J: Class path contains multiple S ...

  9. 【巨杉数据库SequoiaDB】SequoiaDB 巨杉数据库 v3.4 版本正式发布

    深秋时节,SequoiaDB 巨杉数据库在深秋给大家带来了“一把火”.SequoiaDB v3.4 正式发布啦! 分布式交易场景性能大幅提升 SequoiaDB 巨杉数据库3.4版本正式发布,v3.4 ...

  10. 网页出现横向滚动条的原因可能是使用bootstrap不当引起

    Bootstrap的栅格布局超级方便我们写网页.但是在不是全体配合使用的情况下,会出现横向滚动条的现象. 什么叫不是配合使用的情况呢? >>一种情况是:你使用了row作为你的第一层父元素: ...