hdu2196
基本的树形dp,需要dfs三次,第一次求每个点最远的后代,第二次和第三次每个点的孩子分别从左到右和从右到左遍历。
#include <cstdio>
#include <vector>
using namespace std; #define D(x) const int MAX_N = (int)(1e4) + ; int n;
vector <pair<int, int> > edge[MAX_N];
int child[MAX_N];
int father_left[MAX_N];
int father_right[MAX_N]; void dfs_child(int father, int u)
{
child[u] = ;
for (int i = ; i < (int)edge[u].size(); i++)
{
int v = edge[u][i].first;
int w = edge[u][i].second;
if (v != father)
{
dfs_child(u, v);
child[u] = max(child[u], child[v] + w);
}
}
} void dfs_father_left(int father, int u, int up)
{
father_left[u] = up;
int temp = ;
for (int i = ; i < (int)edge[u].size(); i++)
{
int v = edge[u][i].first;
int w = edge[u][i].second;
if (v != father)
{
dfs_father_left(u, v, max(up, temp) + w);
temp = max(temp, w + child[v]);
}
}
} void dfs_father_right(int father, int u, int up)
{
father_right[u] = up;
int temp = ;
for (int i = (int)edge[u].size() - ; i >= ; i--)
{
int v = edge[u][i].first;
int w = edge[u][i].second;
if (v != father)
{
dfs_father_right(u, v, max(up, temp) + w);
temp = max(temp, w + child[v]);
}
}
} int main()
{
while (scanf("%d", &n) != EOF)
{
for (int i = ; i <= n; i++)
{
edge[i].clear();
}
for (int i = ; i <= n; i++)
{
int a, b;
scanf("%d%d", &a, &b);
edge[i].push_back(make_pair(a, b));
edge[a].push_back(make_pair(i, b));
}
dfs_child(-, );
dfs_father_left(-, , );
dfs_father_right(-, , );
for (int i = ; i <= n; i++)
{
D(printf("%d %d %d\n", child[i], father_left[i], father_right[i]));
printf("%d\n", max(child[i], max(father_left[i], father_right[i])));
}
}
return ;
}
hdu2196的更多相关文章
- 【hdu2196】Computer
hdu 2196 computer 题意 给你一棵树,边有权值. 对于每一个点,求其与其距离最远的点的距离. 分析 思路1:树的直径 利用直径的性质进行求解,网上资料很多,这里不赘述. #includ ...
- 【树形dp小练】HDU1520 HDU2196 HDU1561 HDU3534
[树形dp]就是在树上做的一些dp之类的递推,由于一般须要递归处理.因此平庸情况的处理可能须要理清思路.昨晚開始切了4题,作为入门训练.题目都很easy.可是似乎做起来都还口以- hdu1520 An ...
- hdu2196 树形dp
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 Problem Description A school bought the fi ...
- 【HDU2196 Computer】经典树形dp
http://acm.hdu.edu.cn/showproblem.php?pid=2196 题意:有n台电脑相连,让你求每台电脑与离它最远的那台电脑的距离. 思路:两遍搜索即可,第一遍从上到下,第二 ...
- HDU2196 Computer(树形DP)
和LightOJ1257一样,之前我用了树分治写了.其实原来这题是道经典的树形DP,感觉这个DP不简单.. dp[0][u]表示以u为根的子树中的结点与u的最远距离 dp[1][u]表示以u为根的子树 ...
- hdu2196 树的直径 + bfs
//Accepted 740 KB 15 ms //树的直径 //距离一个顶点最远的点一定是树的直径的一个端点 #include <cstdio> #include <cstring ...
- HDU-2196 Computer (树形DP)
题目大意:在一棵带边权的有根树中,对于每个点,找出它与离它最远的那个点的之间的距离. 题目分析:对于一个点,离它最远的点只有两种情况,一是它到叶子节点的最远距离,一是与它父亲的距离加上他的父亲到叶子节 ...
- HDU2196 - Computer(树形DP)
题目大意 给定一颗n个结点的树,编号为1~n,要求你求出每个结点能到达的最长路径 题解 用动态规划解决的~~~~把1 当成树根,这样就转换成有根树了.我们可以发现,对于每个结点的最长路,要么是从子树得 ...
- hdu2196 树形dp经典|树的直径
/* 两种做法 1.求出树直径v1,v2,那么有一个性质:任取一点u,树上到u距离最远的点必定是v1或v2 那么可以一次dfs求树v1 第二次求dis1[],求出所有点到v1的距离,同时求出v2 第三 ...
随机推荐
- vim查找替换
http://www.cnblogs.com/ltang/articles/2034291.html %: 表示百分百,表示所有 行的意思... 如果不指定行号, 则表示当前行; g: 表示一行中的所 ...
- mysql explain详解
对于经常使用mysql的兄弟们,对explain一定不会陌生.当你在一条SELECT语句前放上关键词EXPLAIN,MySQL解释它将如何处理SELECT,提供有关表如何联合和以什么次序的信息.借助于 ...
- CF459A Pashmak and Garden (水
Pashmak and Garden Codeforces Round #261 (Div. 2) A. Pashmak and Garden time limit per test 1 second ...
- [译]git fetch
git fetch从远程仓储导入commit到你的本地仓储. 这些fetch到的commit是做为一个远程分支存储在你本地的. 这样你可以在集成这些commit到你的项目前先看看都有些什么修改. 用法 ...
- Backbone☞View中的events...click事件失效
<div id="container"> <input type="button" id="test_click" val ...
- 1_mysql +DBA职业发展
MYSQL + DBA 职业发展 mysql :the world's most popular open source database 最流行的开源数据库 数据库世界 关系数据库(又称SQL数据库 ...
- mybatis 多个dao重名,根据namespace解析
在mybatis通过执行sql语句的方式是,用getSqlSession().xxx(param,..)方法来调用, 其中第一个参数就是dao mapper.xml文件的命名空间.id package ...
- 下位机多个".c, .h"文件的相互包含及排版
一.背景: 自从接触单片机编程以来,由于工作上的需要,不可避免的时常会接手别人的代码,但常常由于上一位同事的编码随意性有点大,导致可读性非常的差,有时候不得不完全舍弃原有代码,推倒重来,无形中增加了工 ...
- cf251.2.C (构造题的技巧)
C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...
- 自定义C/C++头文件以及头文件重复定义解决
今天再看二叉树的知识,看着看着就看到C/C++的头文件及头文件重复定义这一块去了.以前就看到过这个问题,但是自己一直没有用到这方面的东西,今天遇到就顺便总结一下,等以后忘了再回来看看. 首先明确一点C ...