题意:

给定一棵树, 求树的直径。

分析:

两种方法:

1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径。

2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次长距离, 然后某个点的最长+次长就是直径。

#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std; const int maxn = + ;
const int inf = 1e9 + ;
int Max[maxn];// 最大距离
int sMax[maxn];// 次大距离
struct Edge {
int v, d;
};
vector<Edge> G[maxn];
int N = ;
void init() {
for(int i = ; i < maxn; i ++) {
G[i].clear();
}
}
void dfs1(int u, int pre) {//更新u本身
Max[u] = sMax[u] = ;
for(int i = ; i < G[u].size(); i++) {
int v = G[u][i].v, d = G[u][i].d;
if(v == pre)
continue; //不经过父亲, 只搜索子树
dfs1(v, u); //一直搜到叶子再回溯, 因为是根据孩子的Max更新自己的Max
if(sMax[u] < Max[v] + d) { //如果u的次长距离 < 到v的距离 + v的最大距离
//更新距离
sMax[u] = Max[v] + d;
if(sMax[u] > Max[u]) { //如果次长距离大于最长距离, 交换二者位置
swap(sMax[u], Max[u]);
}
}
}
} int u, v, d;
int main() {
while(cin >> u >> v >> d) {
N++;
G[u].push_back((Edge) {v,d});
G[v].push_back((Edge) {u,d});
}
dfs1(, -); int ans = -inf;
for(int i = ; i <= N; i++) {
ans = max(ans, Max[i] + sMax[i]);
}
cout << ans << "\n";
return ;
}

POJ 2631 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

    题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...

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

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

  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 (自由树的直径)

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

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

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

  7. 【POJ2631】Roads in the North 树的直径

    题目大意:给定一棵 N 个节点的边权无根树,求树的直径. 代码如下 #include <cstdio> #include <algorithm> using namespace ...

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

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  9. 题解报告:poj 2631 Roads in the North(最长链)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

随机推荐

  1. 线段树(单点更新) POJ 2886 Who Gets the Most Candies?

    题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ...

  2. 516 Longest Palindromic Subsequence 最长回文子序列

    给定一个字符串s,找到其中最长的回文子序列.可以假设s的最大长度为1000. 详见:https://leetcode.com/problems/longest-palindromic-subseque ...

  3. neo4j(图数据库)是什么?

    不多说,直接上干货! 作为一款强健的,可伸缩的高性能数据库,Neo4j最适合完整的企业部署或者用于一个轻量级项目中完整服务器的一个子集存在. 它包括如下几个显著特点: 完整的ACID支持 高可用性 轻 ...

  4. Retrofit Upload multiple files and parameters

    Retrofit 的介绍以及基本使用 这里不再说明. 关于多文件上传 以及上传文件的同时携带多个参数说明 网上涉及到的不是太多. 上一张帅图: 代码: apiService: /** params 参 ...

  5. dangerouslySetHTML 和 style 属性

    这一节我们来补充两个之前没有提到的属性,但是在 React.js 组件开发中也非常常用,但是它们也很简单. dangerouslySetHTML 出于安全考虑的原因(XSS 攻击),在 React.j ...

  6. P1936 水晶灯火灵

    题目描述 ,刚好符合以下③条规则:①m.n∈{1,2,……,k}②(n²-m*n-m²)²=1③m.n为整数. 输入输出格式 输入格式: Only one:k.(What?还是失败?) 输出格式: 共 ...

  7. 回顾PMP考试

    2014年9月20日,于我来说绝对可以说是一个重要的日子.经过考场里4个多小时(4个小时正式的时间+前面的签到以及后面的survey等)的鏖战,出去之后才发现北京外国语大学的楼宇是如此的漂亮,阳光也是 ...

  8. 手工修改注册表激活windows xp法

    手工修改注册表激活windows xp法: 1.安装原版windows xp 2.打开注册表regedit 3.找到主键 Hkey_Local_Machine\Software\Microsoft\W ...

  9. Microsoft Windows Server 系统基本配置

    Microsoft Windows Server 系统基本配置 环境基本配置 桌面和显示属性 更新服务器名称 更新用户密码 创建密码重置盘 设置网络类型 TCP/IP设置 ping和ipconfig命 ...

  10. PHP04 HTTP协议入门(转)

    HTTP 协议是互联网的基础协议,也是网页开发的必备知识,最新版本 HTTP/2 更是让它成为技术热点. 本文介绍 HTTP 协议的历史演变和设计思路. 一.HTTP/0.9 HTTP 是基于 TCP ...