题意:

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

分析:

两种方法:

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. AtCoder Grand Contest 016 E - Poor Turkeys

    题目传送门:https://agc016.contest.atcoder.jp/tasks/agc016_e 题目大意: 有\(N\)只火鸡,现有\(M\)个人,每个人指定了两只火鸡\(x,y\),每 ...

  2. 树状数组+二分||线段树 HDOJ 5493 Queue

    题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...

  3. h5-16-插入SVG图片

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 506 Relative Ranks 相对名次

    给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silve ...

  5. Unity项目学习笔记

    1.TCP和IP IP:主要作用是在复杂的网络环境中将数据包发送给的最终的目标地址. 端口号:系统会分给系统端口号  一般知名的端口号在0-1023之间,而我们经常使用的自定义/动态分配的端口号则一般 ...

  6. 关于line-height的理解(如何实现psd稿件上下文字距离为10px)

  7. Qt中为QPushButton添加背景图片

    有2种方式,一种是在代码中设置,另外一种是直接在Qt Creator中直接设置,下面是第二种 参考: http://doc.qt.io/qt-4.8/stylesheet-examples.html ...

  8. sqlserver:查询锁住sql以及解锁

    --查看被锁表:SELECT request_session_id spid, OBJECT_NAME( resource_associated_entity_id ) tableNameFROM s ...

  9. linux下C的建立、编译和运行 gcc (附上Windows下visual c++的用法)

    2019/6/24 1. 环境:window10下安装了MobaXterm,这里申请了阿里云的服务账号,可以直接使用linux系统,避免安装虚拟机等. 2. 判断linux下是否有GCC编译工具(我们 ...

  10. uva12099 The Bookcase

    这道题超经典.dp和优化都值得看一看.因为i+1只和i有关,用滚动数组节省空间暑假第一次做感觉很困难,现在看就清晰了很多 #include<cstdio> #include<cstr ...