HDU-2196-Computer(树上DP)
链接:
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)的更多相关文章
- HDU 2196 Computer (树dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- HDU 2196 Computer (树上最长路)【树形DP】
<题目链接> 题目大意: 输出树上每个点到其它点的最大距离. 解题分析: 下面的做法是将树看成有向图的做法,计算最长路需要考虑几种情况. dp[i][0] : 表示以i为根的子树中的结点与 ...
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- hdu 2196 Computer(树形DP)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2196 Computer( 树上节点的最远距离 )
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2196 Computer 树形dp模板题
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 2196 Computer 树形DP 经典题
给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...
- hdu 2196 Computer(树形DP经典)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 题解报告:hdu 2196 Computer(树形dp)
Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du ...
随机推荐
- SQL Server 从Excel导入到数据库操作遇到的科学计数法问题
问题描述 今天在做从Excel导入数据到SQL Server 中将数据更新到表中,可惜就这一个简单的操作中出现了一点小插曲,就在我根据Excel中的编号关联表编号以此更新姓名字段时出现转换错误问题.如 ...
- [转帖]Java中重写和重载与多态的关系
Java中重写和重载与多态的关系 2019-09-05 00:57:41 留下一天今天 阅读数 67 收藏 更多 分类专栏: java进阶之路 版权声明:本文为博主原创文章,遵循CC 4.0 B ...
- Appium移动端测试--基础预热
目录 Android自动化环境准备 需要安装的软件: Appium多端架构与自动化 Android自动化前提依赖: 获取App的信息: Android常用命令 adb shell 常用命令列表: An ...
- xorm-删除和软删除实例
删除数据Delete方法,参数为struct的指针并且成为查询条件.注意:当删除时,如果user中包含有bool,float64或者float32类型,有可能会使删除失败 package main i ...
- 【转】ZYNQ中三种实现GPIO的方式
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/husipeng86/article/det ...
- 记一次node爬虫经历,手把手教你爬虫
今天业务突然来了个爬虫业务,爬出来的数据以Excel的形式导出,下班前一个小时开始做,加班一个小时就做好了.因为太久没做爬虫了!做这个需求都是很兴奋! 需求说明 访问网站 (循环)获取页面指定数据源 ...
- Docker 使用入门 Hello World
结合上一篇的所说的内容:现在安装好了Docker 安装好了就要用嘛,所以,先走一个Hello World docker run ubuntu:15.10 /bin/echo "hello w ...
- java之spring之整合ssh-2
这篇也是主要讲解 ssh 的整合,不同于上一篇的是它的注入方式. 这篇会采用扫描注入的方式,即去除 applicationContext-asd.xml 文件. 目录结构如下: 注意,这里只列举不同的 ...
- 2017-07-25 PDO预处理以及防止sql注入
首先来看下不做任何处理的php登录,首先是HTML页面代码 <html> <head><title>用户登录</title></head> ...
- 【i.MX6UL/i.MX6ULL开发常见问题】单独编译内核,uboot生成很多文件,具体用哪一个?
[i.MX6UL/i.MX6ULL开发常见问题]2.3单独编译内核,uboot生成很多文件,具体用哪一个? 答:内核编译出来的文件是~/MYiR-imx-Linux/arch/arm/boot/目录下 ...