题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196

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

总算彻底理解这个题了 =.=

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=; struct Edge
{
int v;
int len;
int next;
} edge[maxn<<]; int head[maxn];
int fm[maxn],fn[maxn]; ///最远距离,以及对应的子树的序号
int sm[maxn],sn[maxn]; ///次远距离,以及其对应的子树
int k; void addedge(int u,int v,int l)
{
edge[k].v=v;
edge[k].len=l;
edge[k].next=head[u];
head[u]=k++; edge[k].v=u;
edge[k].len=l;
edge[k].next=head[v];
head[v]=k++;
} void dfs1(int u,int p) ///第一次dfs找到子树种最大的长度,和最大长度的子树的序号
{
fm[u]=; ///初始化
sm[u]=;
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(v==p) continue;
dfs1(v,u);
if(edge[i].len+fm[v]>sm[u])
{
sm[u]=edge[i].len+fm[v];
sn[u]=v;
if(sm[u]>fm[u])
{
swap(fm[u],sm[u]);
swap(fn[u],sn[u]);
}
}
}
} void dfs2(int u,int p)
{
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(v==p) continue;
if(v==fn[u])
{
if(edge[i].len+sm[u]>sm[v])
{
sm[v]=edge[i].len+sm[u];
sn[v]=u; ///开始没懂为什么还要记录结点
if(sm[v]>fm[v]) ///更新的目的就是可以找到从子树来的最大值,不记录有时候找的是次大值
{
swap(fm[v],sm[v]);
swap(fn[v],sn[v]);
}
}
}
else
{
if(edge[i].len+fm[u]>sm[v])
{
sm[v]=edge[i].len+fm[u];
sn[v]=u;
if(sm[v]>fm[v])
{
swap(fm[v],sm[v]);
swap(fn[v],sn[v]);
}
}
}
dfs2(v,u);
}
} int main()
{
int n;
while(scanf("%d",&n)==)
{
memset(head,-,sizeof(head));
k=;
int x,l;
for(int i=; i<=n; i++)
{
scanf("%d%d",&x,&l);
addedge(i,x,l);
}
dfs1(,-); ///随便选一点作为树根,那么他的父亲结点就设为-1
dfs2(,-); ///所以换成dfs1(2,-1),dfs2(2,-1) 也是可以的
for(int i=; i<=n; i++)
printf("%d\n",fm[i]);
}
return ;
}

hdu2196 树形dp的更多相关文章

  1. hdu2196 树形dp经典|树的直径

    /* 两种做法 1.求出树直径v1,v2,那么有一个性质:任取一点u,树上到u距离最远的点必定是v1或v2 那么可以一次dfs求树v1 第二次求dis1[],求出所有点到v1的距离,同时求出v2 第三 ...

  2. hdu2196树形dp

    有一棵树,找每个节点所能到达的最远距离是多少 dis[u][0]正向最大距离    dis[u][1]正向次大距离     dis[u][2]反向最大距离 先一边dfs求出每个节点的正向最大距离(就是 ...

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

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

  4. hdu-2196 树形dp 求一个树中所有节点能到达的最远距离f[i] (其实也不难嘛!)

    #include <bits/stdc++.h> using namespace std; ; struct T { int to; int w; }; vector < vecto ...

  5. 【树形dp小练】HDU1520 HDU2196 HDU1561 HDU3534

    [树形dp]就是在树上做的一些dp之类的递推,由于一般须要递归处理.因此平庸情况的处理可能须要理清思路.昨晚開始切了4题,作为入门训练.题目都很easy.可是似乎做起来都还口以- hdu1520 An ...

  6. HDU2196 Computer(树形DP)

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

  7. HDU5834 Magic boy Bi Luo with his excited tree(树形DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also ...

  8. Codeforces 543D Road Improvement(树形DP + 乘法逆元)

    题目大概说给一棵树,树的边一开始都是损坏的,要修复一些边,修复完后要满足各个点到根的路径上最多只有一条坏的边,现在以各个点为根分别求出修复边的方案数,其结果模1000000007. 不难联想到这题和H ...

  9. POJ3162 Walking Race(树形DP+尺取法+单调队列)

    题目大概是给一棵n个结点边带权的树,记结点i到其他结点最远距离为d[i],问d数组构成的这个序列中满足其中最大值与最小值的差不超过m的连续子序列最长是多长. 各个结点到其他结点的最远距离可以用树形DP ...

随机推荐

  1. <<< Oracle系统参数命令、服务进程、默认用户

    系统参数命令 1.ALTER SYSTEM SET nls_language=american; //设置环境语言为英文 2.SHOW PARAMETER p_name; //显示系统参数 db_na ...

  2. 搭建FTP服务器

    yum install vsftpd -yyum install pam* db4* --skip-broken –y 创建并生成vsftpd 数据库文件vi /etc/vsftpd/ftpusers ...

  3. PHP 图片上传

    PHP上传的简单案例: Html文件: <html> <form action="index.php" name="form" method= ...

  4. [Nhibernate]体系结构

    引言 在项目中也有用到过nhibernate但对nhibernate的认识,也存留在会用的阶段,从没深入的学习过,决定对nhibernate做一个系统的学习. ORM 对象-关系映射(OBJECT/R ...

  5. python 学习 : 一个简单的秒表

      游戏说明:绿色数字(左边表示成功停止在整秒的次数,右边表示停止的总次数) 点击stop,如果小数点后为0,即你停止的时间是整秒数,右上方斜杠左边数字加一 把代码复制到这个网页code run he ...

  6. codevs1183 泥泞的道路

    题目描述 Description CS有n个小区,并且任意小区之间都有两条单向道路(a到b,b到a)相连.因为最近下了很多暴雨,很多道路都被淹了,不同的道路泥泞程度不同.小A经过对近期天气和地形的科学 ...

  7. (二)SQL Server分区创建过程

    虽然分区有很多好处(一)SQL Server分区详解Partition,却不能随意使用:且不说分区管理的繁琐,只是跨分区带来的负面影响就需要我们好好分析是否有必要使用分区.一般分区创建的业务特点:用于 ...

  8. nyoj 218 Dinner(贪心专题)

    Dinner 时间限制:100 ms  |  内存限制:65535 KB 难度:1   描述 Little A is one member of ACM team. He had just won t ...

  9. Linux学习之一--VI编辑器的基本使用

    vi编辑器是Linux系统下标准的编辑器.而且不逊色于其他任何最新的编辑器.可是会用的有多少呢.下面介绍一下vi编辑器的简单用法和部分命令.让你在Linux系统中畅行无阻. 基本上vi可以分为三种状态 ...

  10. 好代码系列(一):LazyObject

    site-packages/django/utils/functional.py def new_method_proxy(func): def inner(self, *args): if self ...