题目大意:给定一棵 N 个节点的边权无根树,求树的直径。

代码如下

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1e4+10; struct node{
int nxt,to,w;
}e[maxn<<1];
int tot=1,head[maxn];
inline void add_edge(int from,int to,int w){
e[++tot].nxt=head[from],e[tot].to=to,e[tot].w=w,head[from]=tot;
}
int d1[maxn],d2[maxn],ans; void dfs(int u,int fa){
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to,w=e[i].w;if(v==fa)continue;
dfs(v,u);
if(d1[v]+w>d1[u])d2[u]=d1[u],d1[u]=d1[v]+w;
else d2[u]=max(d2[u],d1[v]+w);
}
} void solve(){
dfs(1,0);
for(int i=1;i<=10000;i++)ans=max(ans,d1[i]+d2[i]);
printf("%d\n",ans);
} int main(){
int from,to,w;
while(scanf("%d%d%d",&from,&to,&w)!=EOF){
add_edge(from,to,w),add_edge(to,from,w);
}
solve();
return 0;
}

【POJ2631】Roads in the North 树的直径的更多相关文章

  1. POJ 2631 Roads in the North(树的直径)

    POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...

  2. poj--2631--Roads in the North(树的直径 裸模板)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2389   Accepted: 117 ...

  3. Codeforces 1413F - Roads and Ramen(树的直径+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 其实是一道还算一般的题罢--大概是最近刷长链剖分,被某道长链剖分与直径结合的题爆踩之后就点开了这题. 本题的难点就在于看出一个性质:最长路 ...

  4. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  5. POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)

    题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...

  6. poj 2631 Roads in the North (自由树的直径)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4513   Accepted: 215 ...

  7. POJ 2631 Roads in the North (模板题)(树的直径)

    <题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...

  8. POJ 2631 Roads in the North (树的直径)

    题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...

  9. poj1985 / poj2631(树的直径)

    poj1985 Cow Marathon 树的直径裸题 树的直径的一般求法: 任意一点为起点,dfs/bfs找出与它最远的点$u$ 以$u$为起点,dfs/bfs找出与它最远的点$v$ 则$d(u,v ...

随机推荐

  1. 20155223 Exp9 Web安全基础实践

    20155223 Exp9 Web安全基础实践 基础问题回答 SQL注入攻击原理,如何防御? 攻击原理:SQL注入即是指web应用程序对用户输入数据的合法性没有判断,攻击者可以在web应用程序中事先定 ...

  2. 汇编 do while循环

    do while生成的汇编代码  do while汇编还原成C++代码 一. do while成生的汇编代码 // int i=0; // do // { // i++; // } while ( ...

  3. EJB开发第一期---EJB开发配置

    一.EJB 3.0简介 1.1 什么是EJB Enterprise JavaBeans是一个用于分布式业务应用的标准服务端组件模型.采用Enterprise JavaBeans架构编写的应用是可伸缩的 ...

  4. RHEL6 最小化系统 编译安装部署zabbix (mysql)

    RHEL6 最小化系统 编译安装部署zabbix (mysql)官方说明详细见:https://www.zabbix.com/documentation/4.0/manual/installation ...

  5. springboot 设置 session 过期时间

    application.properties server.session.timeout=86400 #单位(s) 这里是24小时

  6. PAT甲题题解-1027. Colors in Mars (20)-水。。。

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  7. Linux内核分析 笔记三 构造一个简单的Linux系统MenuOS ——by王玥

    一.知识点总结 (一)Linux源代码简介 arch/x86目录下的代码是我们重点关注的 内核启动相关代码都在init目录下 start_kernel函数相当于普通C程序的main函数 linux的核 ...

  8. 《Linux内核分析》第二周:操作系统是如何工作的

    杨舒雯 20135324 北京电子科技学院 杨舒雯 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1 ...

  9. linux下php环境的装配以及php storm的链接

    linux下php环境的装配以及php storm的链接 本次安装在deepin系统下完成 一.安装LAMP组合 Linux+Apache+Mysql+php 直接命令 sudo apt-get in ...

  10. Spark 实践——用 Scala 和 Spark 进行数据分析

    本文基于<Spark 高级数据分析>第2章 用Scala和Spark进行数据分析. 完整代码见 https://github.com/libaoquan95/aasPractice/tre ...