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 ... 
随机推荐
- 51nod1201 整数划分
			01背包显然超时.然后就是一道神dp了.dp[i][j]表示j个数组成i的方案数.O(nsqrt(n)) #include<cstdio> #include<cstring> ... 
- android studio 加载第三方类库
			以引入Xutil包为例 1. 将Xutil包导入到module的libs目录下 2. File->project structure 还有一种方法是在libs目录下右键点击Add as libr ... 
- Heritrix源码分析(五) 如何让Heritrix在Ecplise等IDE下编程启动(转)
			本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/642550 本博客已迁移到本人独立博客: http://www.yun5u. ... 
- Java异常体系结构
			1)系统错误(system error)是由Java虚拟机抛出的,用Error类表示.Error类描述的是内部系统错误.这样的错误很少发生.如果发生,除了通知用户以及尽量稳妥地终止程序外,几乎什么都不 ... 
- PHP String函数分类
			1.查找字符位置函数: strpos ($str,search,[int]): 查找search在$str中的第一次位置从int开始: stripos ($str,search,[int]): ... 
- 实用js+css多级树形展开效果导航菜单
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ... 
- JSONP实例
			JSONP实例 package sus.app; import java.io.IOException; import java.util.Map; import javax.servlet.http ... 
- 可进行JavaScript代码测试与调试的12个网站
			概述:JavaScript是网站前端开发最为重要的一门编程语言,本文收集了能够在线测试与调试JavaScript代码的12个网站 1.JS Bin JS bin是一个为JavaScript和CSS爱好 ... 
- Initializing a Build Environment
			This section describes how to set up your local work environment to build the Android source files. ... 
- Javascript 日期时间格式正则
			因为Javascript的日期格式判断可能因浏览器的版本有所不同,所以用正则判断会比较好,这里备注一个正则用来判断日期时间的格式: ^(?=\d)(?:(?!(?:1582(?:\.|-|\/)10( ... 
