链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2196

题意:

A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information.

Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.

思路:

可以一眼看出可以用换根.

但是无法处理是否上一个节点的最长距离正好选了某个子节点,看了题解发现可以去记录最长的和次长的,长见识了。。

一次DFS求出有向的每个点往下的最长距离和次长距离,然后再一次DFS,求出往父节点扩展的长度。

代码:

// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MOD = 1e9;
const int MAXN = 1e4+10; struct Node
{
int x;
int v;
};
vector<Node> G[MAXN];
int Dp1[MAXN], Dp2[MAXN], Dp3[MAXN];
int Max[MAXN];
int n; void Dfs1(int x, int pre)
{
Dp1[x] = Dp2[x] = 0;
for (int i = 0;i < G[x].size();i++)
{
int node = G[x][i].x;
if (node == pre)
continue;
Dfs1(node, x);
if (Dp1[node]+G[x][i].v > Dp1[x])
{
Dp1[x] = Dp1[node]+G[x][i].v;
Max[x] = node;
}
}
for (int i = 0;i < G[x].size();i++)
{
int node = G[x][i].x;
if (node == Max[x])
continue;
Dp2[x] = max(Dp2[x], Dp1[node]+G[x][i].v);
}
} void Dfs2(int x, int pre)
{
for (int i = 0;i < G[x].size();i++)
{
int node = G[x][i].x;
if (node == pre)
continue;
if (node != Max[x])
Dp3[node] = max(Dp1[x]+G[x][i].v, Dp3[x]+G[x][i].v);
else
Dp3[node] = max(Dp2[x]+G[x][i].v, Dp3[x]+G[x][i].v);
Dfs2(node, x);
}
} int main()
{
// freopen("test.in", "r", stdin);
int t, cas = 0;
// scanf("%d", &t);
while(~scanf("%d", &n))
{
for (int i = 1;i <= n;i++)
G[i].clear();
memset(Dp1, 0, sizeof(Dp1));
memset(Dp2, 0, sizeof(Dp2));
int a, l;
for (int i = 2;i <= n;i++)
{
scanf("%d%d", &a, &l);
G[a].push_back(Node{i, l});
G[i].push_back(Node{a, l});
}
Dfs1(1, -1);
Dfs2(1, -1);
for (int i = 1;i <= n;i++)
printf("%d\n", max(Dp1[i], Dp3[i]));
} return 0;
}

HDU-2196-Computer(树上DP)的更多相关文章

  1. HDU 2196 Computer (树dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...

  2. HDU 2196 Computer (树上最长路)【树形DP】

    <题目链接> 题目大意: 输出树上每个点到其它点的最大距离. 解题分析: 下面的做法是将树看成有向图的做法,计算最长路需要考虑几种情况. dp[i][0] : 表示以i为根的子树中的结点与 ...

  3. HDU 2196.Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. HDU 2196 Computer 树形DP经典题

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...

  5. hdu 2196 Computer(树形DP)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. HDU 2196 Computer( 树上节点的最远距离 )

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. hdu 2196 Computer 树形dp模板题

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  8. HDU 2196 Computer 树形DP 经典题

    给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...

  9. hdu 2196 Computer(树形DP经典)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  10. 题解报告:hdu 2196 Computer(树形dp)

    Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du ...

随机推荐

  1. [转帖]UML类图新手入门级介绍

    UML类图新手入门级介绍 2010-11-12 19:45:00 monkey_d_meng 阅读数 27230  收藏 文章标签: umlinterfaceclass编程扩展更多 分类专栏: 软件工 ...

  2. 数论 - 同余 + BFS (Find The Multiple)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16995   Accepted: 692 ...

  3. 关于于c++中的类型转换

    隐藏式类型转换 void test() { ; ; a = b; //此时发生的是默认的类型转 //(据说编译器是微软的编译器是不允许编译通过) std::cout << a <&l ...

  4. EasyExcel写文件

    公共部分 HttpServletResponse // 需要处理response HttpServletResponse response; response.reset(); response.se ...

  5. matplotlib 中文乱码问题

    matplotlib是Python著名的绘图库,默认并不支持中文显示,因此在不经过修改的情况下,无法正确显示中文. 本文将介绍解决这一问题的方法. 不修改文件,加两行代码即可: matplotlib. ...

  6. kali之使用sqlmap进行sql注入

    sqlmap简介 sqlmap支持五种不同的注入模式: 1.基于布尔的盲注,即可以根据返回页面判断条件真假的注入. 2.基于时间的盲注,即不能根据页面返回内容判断任何信息,用条件语句查看时间延迟语句是 ...

  7. Vue项目开发相关问题总结

    Vue项目开发相关问题总结 一.创建一个项目(两种方式) 1.通过CLI命令行创建,具体步骤如下: (1)Node 版本要求 Vue CLI 需要 Node.js 8.9 或更高版本 (推荐 8.11 ...

  8. 原生js 定义分页控件,类似于百度搜索

    实现一个类似于百度搜索结果的分页样式,样式可以自定义,接近于原生,少部分Jquery . 1.实现效果截图(默认无任何样式)  2.主要程序代码 define(function (require, e ...

  9. 区块链会2020再次爆发,先学点DAPP压压惊,跟我一起学《区块链DApp入门实战》

    区块链DApp正在经历市场洗礼,常言道,对抗动荡最稳妥的是稳扎稳打的技术学习,不能临时抱佛脚. 马化腾说:互联网会像水和电一样融入我们的生活.而区块链呢?它是价值互联网的基石,是未来必然的趋势,也会像 ...

  10. Xcode调试打印方法

    1 NSLog 在调试的过程中,最常用的查看变量值的方法是NSLog 整数 int a = 1; NSLog("%d", a); 浮点数 float b = 1.11; NSLog ...