链接:

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. [转帖]BurpSuite简介

    BurpSuite简介 https://bbs.ichunqiu.com/thread-54760-1-1.html BurpSuite ,这是一个辅助渗透的工具,可以给我们带来许多便利.Burp 给 ...

  2. 01-Windows Server 2012的配置与部署

    一. 背景 这里以阿里云Windows Server 2012系统的服务器为主,介绍服务器的配置以及.Net程序的发布顺序,在后续的项目管理文章中,会介绍<运维手册>的写法. 二. 步骤 ...

  3. go包管理

    摘自: http://blueskykong.com/2019/02/18/go-dep-1/ https://www.cnblogs.com/apocelipes/p/10295096.html#v ...

  4. jquery跨js文件调用函数示例

    var common_func; (function() { common_func = { load_hot_data: function(AreaCode) { var hot_html = &q ...

  5. 把项目通过maven生产源码包和文档包并发布到自己的私服上

    <!-- 把项目通过maven生产源码包和文档包并发布到自己的私服上 执行maven命令,mvn clean package,执行完成后 命令:mvn deploy 就可以发布到你自己的私服上了 ...

  6. 浅谈JS中 var let const 变量声明

    浅谈JS中 var let const 变量声明 用var来声明变量会出现的问题: 1. 允许重复的变量声明:导致数据被覆盖 2. 变量提升:怪异的数据访问.闭包问题 3. 全局变量挂载到全局对象:全 ...

  7. Linux E667 同步失败

    在使用Vim编辑/proc目录下的文件后,保存,显示"E667 同步失败" 原因 因为proc这个目录是一个虚拟文件系统,它放置的数据都是在内存中,本身不占有磁盘空间,所以使用Vi ...

  8. Synchronized可重入锁通俗易懂的简单分析

    可重入锁概念: 当一个线程得到一个对象锁后,再次请求此对象时时可以再次得到该对象的锁的,这也证明synchronized方法/块的内部调用本类的其他synchronized方法/块时,时永远可以得到锁 ...

  9. WPF应用打包流程

    1,安装工程模板插件Microsoft Visual Studio Installer Projects https://marketplace.visualstudio.com/items?item ...

  10. redis设置远程连接

    1.修改redis服务器的配置文件 本机安装的redis-4.0.14默认的配置文件 redis.conf 设置 绑定本机地址:bind 127.0.0.1 开启保护模式:protected-mode ...