题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257

hdu2196一样,两次dfs

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e5 + ;
struct Edge {
int next, to, cost;
}edge[N << ];
int head[N], tot;
P d[N], pos[N];
int up[N]; void init(int n) {
memset(head, -, sizeof(head));
tot = ;
for(int i = ; i <= n; ++i) {
d[i].first = d[i].second = ;
pos[i].first = pos[i].second = -;
up[i] = ;
}
} inline void add(int u, int v, int cost) {
edge[tot].next = head[u];
edge[tot].to = v;
edge[tot].cost = cost;
head[u] = tot++;
} void dfs1(int u, int p) {
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(v == p)
continue;
dfs1(v, u);
if(d[v].first + edge[i].cost > d[u].first) {
if(d[u].first != )
d[u].second = d[u].first;
d[u].first = d[v].first + edge[i].cost;
pos[u].first = v;
} else if(d[v].first + edge[i].cost > d[u].second) {
d[u].second = d[v].first + edge[i].cost;
pos[u].second = v;
}
}
} void dfs2(int u, int p) {
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(v == p)
continue;
if(v == pos[u].first) {
up[v] = max(up[u], d[u].second) + edge[i].cost;
} else {
up[v] = max(up[u], d[u].first) + edge[i].cost;
}
dfs2(v, u);
}
} int main()
{
int t, n;
scanf("%d", &t);
for(int ca = ; ca <= t; ++ca) {
scanf("%d", &n);
init(n);
int u, v, cost;
for(int i = ; i < n; ++i) {
scanf("%d %d %d", &u, &v, &cost);
add(u, v, cost);
add(v, u, cost);
}
dfs1(, -);
dfs2(, -);
printf("Case %d:\n", ca);
for(int i = ; i < n; ++i) {
printf("%d\n", max(d[i].first, up[i]));
}
}
return ;
}

lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)的更多相关文章

  1. light oj 1094 Farthest Nodes in a Tree(树的直径模板)

    1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...

  2. lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】

    1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  3. LightOJ 1094 - Farthest Nodes in a Tree(树的直径)

    http://acm.hust.edu.cn/vjudge/contest/121398#problem/H 不是特别理解,今天第一次碰到这种问题.给个链接看大神的解释吧 http://www.cnb ...

  4. LightOJ1257 Farthest Nodes in a Tree (II)(树的点分治)

    题目给一棵树,边带有权值,求每一点到其他点路径上的最大权和. 树上任意两点的路径都可以看成是经过某棵子树根的路径,即路径权=两个点到根路径权的和,于是果断树分治. 对于每次分治的子树,计算其所有结点到 ...

  5. Farthest Nodes in a Tree ---LightOj1094(树的直径)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...

  6. Farthest Nodes in a Tree (求树的直径)

    题目链接,密码:hpu Description Given a tree (a connected graph with no cycles), you have to find the farthe ...

  7. LightOJ1094 - Farthest Nodes in a Tree(树的直径)

    http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...

  8. lightoj1094 - Farthest Nodes in a Tree

    1094 - Farthest Nodes in a Tree   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limi ...

  9. E - Farthest Nodes in a Tree

    Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. Th ...

随机推荐

  1. HDU 2125 Local area network

    简单DP,N×M的网格其中有一条边坏掉了,问从起点到终点的放法数 有两种方法,一种是DP很好理解 //#define LOCAL #include <cstdio> #include &l ...

  2. Sublime安装插件的方法

    一:打开:工具--命令面板 二:输入:package,,选择下拉列表里提示的install package 三:输入需要安装的插件的名称,如:angularJS  Less  sass 回车即可安装, ...

  3. 在Datatables中加入错误提示功能

    经常用Datatables的童鞋一定碰到过当采用服务端请求的时候,一旦后台出现异常,Datatables的会一直卡在那里,中间的正在处理的提示一直停留着. 为了能给用户更好的体验,需要对Datatab ...

  4. 《Write Optimized B-Trees》读书报告

    论文原作者:Goetz Graefe, Microsoft.我读完这篇论文后颇有收获,所以写了一篇论文报告,旨在更精炼准确地阐述论文核心思想. 摘要:论文提出了一种方法,这种方法可以优化B树索引写性能 ...

  5. Python time mktime()方法

    描述 Python time mktime() 函数执行与gmtime(), localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数. 如果输入的值不 ...

  6. mysql的password()函数和md5函数

    password用于修改mysql的用户密码,如果是应用与web程序建议使用md5()函数, password函数旧版16位,新版41位,可用select length(password('12345 ...

  7. win7下折腾filezilla_client

    基本上参照 http://blog.csdn.net/iamoyjj/article/details/6358742 但发现这样做的话,engine 生成的 lib中死活都没idn_free 及idn ...

  8. 支持 MBTiles 规范的预缓存

    SuperMap iServer 支持生成符合MBTiles规范的预缓存(MBTiles是由MapBox制定的一种将瓦片地图数据存储到SQLite数据库中并可快速使用,管理和分享的规范. 该规范由Ma ...

  9. 如何在MySql中记录SQL日志

    SQL server有一个sql profiler可以实时跟踪服务器执行的SQL语句,这在很多时候调试错误非常有用.例如:别人写的复杂代码.生产系统.无调试环境.无原代码... ...   查了一下资 ...

  10. base64 encoding

    //https://en.wikipedia.org/wiki/Base64 std::string base64Encode(const std::vector<char>& b ...