Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7324    Accepted Submission(s):
3627

Problem Description
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.

 
Input
Input file contains multiple test cases.In each case
there is natural number N (N<=10000) in the first line, followed by (N-1)
lines with descriptions of computers. i-th line contains two natural numbers -
number of computer, to which i-th computer is connected and length of cable used
for connection. Total length of cable does not exceed 10^9. Numbers in lines of
input are separated by a space.
 
Output
For each case output N lines. i-th line must contain
number Si for i-th computer (1<=i<=N).
 
Sample Input
5
1 1
2 1
3 1
1 1
 
Sample Output
3
2
3
4
4
 
Author
scnu
 
Recommend
lcy
 
题目大意:给定N个点的无根树,求每个点到它最远点的距离。
试题分析:设dp[i]表示i到最远点的距离,我们发现dp[i]=max(dp[i->son]+1,dp[fa[i]]+1)
     但是这个转移方程在父节点的最长路如果是走到i的子树的那么就出错了。
     于是我们添加一维状态:dp[i][2] 表示i号节点的子树中最远点与不在子树中最远点。
     dp[i][0]=max(dp[i->son][0]+1)
     dp[i][1]=max(dp[fa[i]][1]+1,dp[fa[i]][0]+1)
     列出转移方程,发现我们的顾虑并没有消除,我们不确定父节点的在子树中最大的是不是i的子树。
     为了解决这个问题,我们将状态变成dp[i][3]表示i到子树中最远点,i到子树中次远点,不在i的子树中与i距离最远的点
     那么dp[i][0]=max(dp[i->son][0]+1)
       dp[i][1]=max(dp[i->son][0]+1)  (i->son不满足max(dp[i->son][0]))
       dp[i][2]=max(dp[fa[i]][2]+1,dp[fa[i]][0]+1) (dp[fa[i]][0]的节点不在i的子树中)
             max(dp[fa[i]][2]+1,dp[fa[i]][1]+1) (如果在就要退而求其次了)
 
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=20001;
const int INF=999999;
int N,M; int len[MAXN];
vector<int> vec[MAXN];
struct data{
int Son,k;
}dp[MAXN][3];
int fap[MAXN];
int ans=0; void dfs(int x,int fa){
for(int i=0;i<vec[x].size();i++){
if(vec[x][i]!=fa) dfs(vec[x][i],x);
}
for(int i=0;i<vec[x].size();i++){
int son=vec[x][i];
if(son==fa) continue;
if(dp[x][0].k<dp[son][0].k+len[son]) dp[x][0].k=dp[son][0].k+len[son],dp[x][0].Son=son;
}
for(int i=0;i<vec[x].size();i++){
int son=vec[x][i];
if(son==fa||son==dp[x][0].Son) continue;
if(dp[x][1].k<dp[son][0].k+len[son]) dp[x][1].k=dp[son][0].k+len[son];
}
}
void dfs2(int x,int fa){
if(fa!=-1){
dp[x][2].k=dp[fap[x]][2].k+len[x];
if(dp[fap[x]][0].Son!=x) dp[x][2].k=max(dp[x][2].k,dp[fap[x]][0].k+len[x]);
else dp[x][2].k=max(dp[x][2].k,dp[fap[x]][1].k+len[x]);
}
for(int i=0;i<vec[x].size();i++){
if(vec[x][i]!=fa) dfs2(vec[x][i],x);
}
return ;
} int main(){
while(scanf("%d",&N)!=EOF){
for(int i=1;i<=N;i++){
dp[i][0].k=dp[i][1].k=dp[i][2].k=0;
dp[i][0].Son=dp[i][1].Son=0;
vec[i].clear(); fap[i]=0;
len[i]=0;
}
for(int i=2;i<=N;i++){
int fat=read(),ltg=read();
vec[fat].push_back(i);
len[i]=ltg; fap[i]=fat;
}
dfs(1,-1);dfs2(1,-1);
for(int i=1;i<=N;i++) cout<<max(dp[i][0].k,dp[i][2].k)<<endl;
}
}

【树形dp】Computer的更多相关文章

  1. HDU 2196 树形DP Computer

    题目链接:  HDU 2196 Computer 分析:   先从任意一点开始, 求出它到其它点的最大距离, 然后以该点为中心更新它的邻点, 再用被更新的点去更新邻点......依此递推 ! 代码: ...

  2. SGU 149 树形DP Computer Network

    这道题搜了一晚上的题解,外加自己想了半个早上,终于想得很透彻了.于是打算好好写一写这题题解,而且这种做法比网上大多数题解要简单而且代码也比较简洁. 首先要把题读懂,把输入读懂,这实际上是一颗有向树.第 ...

  3. SGU 149. Computer Network( 树形dp )

    题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, d ...

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

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

  5. computer(树形dp || 树的直径)

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

  6. HDU2196 Computer(树形DP)

    和LightOJ1257一样,之前我用了树分治写了.其实原来这题是道经典的树形DP,感觉这个DP不简单.. dp[0][u]表示以u为根的子树中的结点与u的最远距离 dp[1][u]表示以u为根的子树 ...

  7. HDU 2196 Computer 树形DP 经典题

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

  8. Computer(HDU2196+树形dp+树的直径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2196 题目: 题意:有n台电脑,每台电脑连接其他电脑,第i行(包括第一行的n)连接u,长度为w,问你每 ...

  9. HDU 2196 Computer 树形DP经典题

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

  10. 树形DP

    切题ing!!!!! HDU  2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...

随机推荐

  1. spring 那点事

    Spring核心功能 DI(IOC) 何谓DI(IOC) DI(依赖注入)是spring的核心功能之一. Dependency Injection 和 Inversion of Control 其实就 ...

  2. poj 1837 Balance(背包)

    题目链接:http://poj.org/problem?id=1837 Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissi ...

  3. Centos7的iso everything与DVD以及Live的区别

    DVD.ISO 可以用安装程序安装的所有安装包,推荐镜像 Netinstall.iso 从网络安装或者救援系统 Everything.iso 包含centos7的一套完整的软件包,可以用来安装系统或者 ...

  4. nvidia tk1使用记录--基本环境搭建

    前言 项目最开始是在X86+Nvidia(ubuntu+opencv+cuda)平台上实现,达到了期望性能,最近考虑将其移植到嵌入式平台,特别是最近nvidia出了tegra X1,基于和我们使用的g ...

  5. ThinkPHP5 模型 - 事务支持

    使用事务之前,先确保数据库的存储引擎支持事务操作. MyISAM:不支持事务,主要用于读数据提高性能 InnoDB:支持事务.行级锁和并发 Berkeley DB:支持事务 ThinkPHP5 使用事 ...

  6. Yii 1.1.17 一、安装、目录结构、视图、控制器、扩展自定义函数

    这几天了解了一下Yii框架,以简单的博客项目实战入门.大致的实现流程做个记录. 一.Yii 安装与环境检测 从 www.yiiframework.com 获取一份Yii的拷贝,解压到 /wwwroot ...

  7. python基础===Python 迭代器模块 itertools 简介

    本文转自:http://python.jobbole.com/85321/ Python提供了一个非常棒的模块用于创建自定义的迭代器,这个模块就是 itertools.itertools 提供的工具相 ...

  8. python基础===基于requests模块上的协程【trip】

    今天看博客get了一个有趣的模块,叫做 trip     #(pip install  trip) 兼容2.7版本 基于两大依赖包:TRIP: Tornado & Requests In Pa ...

  9. 64_f2

    flxmlrpc-0.1.4-5.fc26.x86_64.rpm 22-May-2017 21:32 57950 flxmlrpc-devel-0.1.4-5.fc26.i686.rpm 22-May ...

  10. 算法题之找出数组里第K大的数

    问题:找出一个数组里面前K个最大数. 解法一(直接解法): 对数组用快速排序,然后直接挑出第k大的数.这种方法的时间复杂度是O(Nlog(N)).N为原数组长度. 这个解法含有很多冗余,因为把整个数组 ...