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 ...
随机推荐
- BZOJ 3624 免费道路
第一反应:这不先0后1做并查集就行了吗? 然后WA了... 哦....啊?哦...233 如果按顺序做并查集,有些0的边可能很重要(只能由它作为0连起两个联通块),但并没有被选. 于是先按1做并查集, ...
- mac出现一个白条
mac出现一个白条,除了finder没有任何程序运行,出现好几次了,怎么解决? 打开finder输中文出现 按esc键
- 【英语】Bingo口语笔记(69) - 脏话的表达
- linux上改变mysql数据文件的位置
用软连接改变了/var/lib/mysql的位置,并设置好mysql.mysql的权限,但是发现还是不能启动. 发现/var/log/mysqld.log 150308 16:16:02 [Warni ...
- 《摇滚南京》——"人生下来就是孤独"
昨天是纪录片<摇滚南京>东南大学站的展映分享会 我不是一个摇滚迷,作为学渣狗看论文.码代码的时候会塞个耳机,平时其实民谣听得更多一点,摇滚觉得有点高大上.所以整好趁着学校有活动,也跟着高大 ...
- AIX 第3章 指令记录
[ssdb01.shz.hn|oracle|/home/oracle]$oslevel -rq --查看系统已安装的维护级别 Known Recommended Maintenance Levels ...
- 【转】IOS中定时器NSTimer的开启与关闭
原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...
- Linux/Unix shell 自动发送AWR report(二)
观察Oracle数据库性能,Oracle自带的awr 功能为我们提供了一个近乎完美的解决方案,通过awr特性我们可以随时从数据库提取awr报告.不过awrrpt.sql脚本执行时需要我们提供一些交互信 ...
- 一:AndEngine的小例子
首先导入架包,下载:http://download.csdn.net/detail/duancanmeng/4060082 lib文件夹中 像我们写android程序entends Activity一 ...
- openlayers加载地图没有图片时有红叉的解决方法
解决方式:设置样式隐藏图片 <style type="text/css"> .olImageLoadError { /*ol2.12 onImageLoadError ...