lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)
题目链接: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)的更多相关文章
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- LightOJ 1094 - Farthest Nodes in a Tree(树的直径)
http://acm.hust.edu.cn/vjudge/contest/121398#problem/H 不是特别理解,今天第一次碰到这种问题.给个链接看大神的解释吧 http://www.cnb ...
- LightOJ1257 Farthest Nodes in a Tree (II)(树的点分治)
题目给一棵树,边带有权值,求每一点到其他点路径上的最大权和. 树上任意两点的路径都可以看成是经过某棵子树根的路径,即路径权=两个点到根路径权的和,于是果断树分治. 对于每次分治的子树,计算其所有结点到 ...
- Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...
- Farthest Nodes in a Tree (求树的直径)
题目链接,密码:hpu Description Given a tree (a connected graph with no cycles), you have to find the farthe ...
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- lightoj1094 - Farthest Nodes in a Tree
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limi ...
- 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 ...
随机推荐
- Typed Message模式与Event Sourcing
引言 在<设计模式沉思录>(Pattern Hatching: Design Patterns Applied,[美]JohnVlissides著)一书的第4章中,围绕事件Message传 ...
- /bin/bash: [xxxx]: command not found
/******************************************************************************** * /bin/bash: [xxxx ...
- gcc/交叉编译
一.gcc编译的情况: 1.linux gcc包含的c/c++编译器,gcc和cc是一样的,c++和g++是一样的,一般c程序就用gcc编译,c++程序就用g++编译. 2.linux gcc的应用: ...
- jQuery - 实时统计输入框输入个数(中文输入法适用)
经常在实时统计文本框输入多少字的时候,有时会出现不及时统计,特别是在中文输入法下. 为了实时准确统计,可以修改代码如下: $(function() { $("#txtT ...
- AJAX异步调用
前台: function readygo(v) { $.ajax({ type: "post", ...
- Linux进程调度策略
linux内核的三种主要调度策略: 1,SCHED_OTHER 分时调度策略, 2,SCHED_FIFO实时调度策略(先到先服务)3,SCHED_RR实时调度策略(时间片轮转) 实时进程将得到优先调用 ...
- ASP.NET Web API 帮助(help)页面上没有 Test API按钮的解决方法
参与一个web API项目时发现它的help页面特别好用,不仅可以根据webapi的方法和注释自动生成帮助文档以方便查阅,还可以在这个页面上测试webapi方法.于是在自己新建项目时也打算将这个hel ...
- [Papers]MHD, $\p_3\pi$, Lebesgue space [Zhang-Li-Yu, JMAA, 2013]
$$\bex \p_3\pi\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=2,\quad \frac{3}{2}\leq q\leq 3 ...
- 运行Python2.x程序报编码错误的解决办法-UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordina not in range(128)[0m
Python编码问题的终极解决方案:在python的Lib\site-packages文件夹下新建一个sitecustomize.py文件,输入: import sys sys.setdefaulte ...
- 上传Test Result和attachment到ALM
之前在HP的时候用ALM,还是很好用的功能很强大的一个测试管理工具,当时用C#依照ALM的API实现了一个上传测试结果的程序,现在贴出来: 这个程序的使用方式很自由,使得ALM几乎可以和所有测试工具做 ...