Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4646    Accepted Submission(s): 2345


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
 
据说是经典的树形dp,我看了很久题解才看懂为什么要记录以该节点为根的其子树范围内的最大距离和次大路,因为对于一个节点来说,可能得到的距离最大的值的路径来自他的子树,或者从他的父节点过来,所以用两次dfs。第一次DFS求出所有节点在他的子树范围内到叶子节点距离的最大距离和次大路,并且记录最大距离和次大路的叶子节点的编号,第二次DFS更新从父节点过来的情况就可以了。因为如果只存最大值的话,判断一个点的从父节点过来的最大值,那么如果他的父节点存的最大值正好是从该点过来的,那么就失去了从父节点过来的状态,这时要用到这个父节点的次大值。
一开始总疑惑为什么要求次短路,原来是父亲节点的转移处会用到,对自身节点没有用。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define maxn 10060
struct node{
int len,to,next;
}e[2*maxn];
int first[maxn],vis[maxn],dist1[maxn],dist2[maxn],ans[maxn];//dist1[i]表示最大路,dist2[i]表示次大路,dist1id[i]表示得到最大路的叶子节点编号,dist2id[i]表示得到次大路的叶子节点编号
int dist1id[maxn],dist2id[maxn];
void dfs(int u)
{
int i,j,t1,t2,flag;
t1=t2=0;
vis[u]=1;
flag=0; for(i=first[u];i!=-1;i=e[i].next){
int v=e[i].to;
if(vis[v])continue;
flag=1;
dfs(v);
int t=e[i].len+dist1[v];
if(t>=dist1[u]){
dist2[u]=dist1[u];
dist1[u]=t;
dist2id[u]=dist1id[u];
dist1id[u]=dist1id[v];
}
else if(t>dist2[u]){
dist2[u]=t;
dist2id[u]=dist1id[v];
}
}
if(flag==0){
dist1[u]=dist2[u]=0;
dist1id[u]=dist2id[u]=u;return; } } void dfs1(int u)
{
int i,j,t,t1,t2;
t1=t2=0;
vis[u]=1;
for(i=first[u];i!=-1;i=e[i].next){
int v=e[i].to;
if(vis[v])continue;
if(dist1id[u]==dist1id[v]){ //这里先看一下父亲节点所求出的最大路的叶子节点编号是不是和当前节点相同
int t=e[i].len+dist2[u];
if(t>=dist1[v]){
dist2[v]=dist1[v];
dist2id[v]=dist1id[v]; dist1[v]=t;
dist1id[v]=dist2id[u];
}
else if(t>dist2[v]){
dist2[v]=t;
dist2id[v]=dist2id[u];
} //要随时更新dist1[i],这里dist1[i]已经不仅是子树范围了,而是全部范围 }
else{
int t=e[i].len+dist1[u];
if(t>=dist1[v]){
dist2[v]=dist1[v];
dist2id[v]=dist1id[v]; dist1[v]=t;
dist1id[v]=dist1id[u];
}
else if(t>dist2[v]){
dist2[v]=t;
dist2id[v]=dist1id[u];
} }
dfs1(v);
}
} int main()
{
int n,m,i,j,c,d;
while(scanf("%d",&n)!=EOF)
{
memset(first,-1,sizeof(first));
int tot=0;
for(i=2;i<=n;i++){
scanf("%d%d",&c,&d);
int u,v;
u=i;v=c;
tot++;
e[tot].next=first[u];e[tot].to=v;e[tot].len=d;
first[u]=tot; tot++;
e[tot].next=first[v];e[tot].to=u;e[tot].len=d;
first[v]=tot;
}
memset(vis,0,sizeof(vis));
memset(dist1,0,sizeof(dist1));
memset(dist2,0,sizeof(dist2));
memset(dist1id,0,sizeof(dist1id));
memset(dist2id,0,sizeof(dist2id));
dfs(1);
memset(vis,0,sizeof(vis));
dfs1(1);
for(i=1;i<=n;i++){
printf("%d\n",dist1[i]);
}
}
return 0;
}


hdu2196 Compute的更多相关文章

  1. C#中DataTable中的Compute方法使用收集

    原文: C#中DataTable中的Compute方法使用收集 Compute函数的参数就两个:Expression,和Filter. Expresstion是计算表达式,关于Expression的详 ...

  2. Compute Resource Consolidation Pattern 计算资源整合模式

    Consolidate multiple tasks or operations into a single computational unit. This pattern can increase ...

  3. 学习OpenStack之(6):Neutron 深入学习之 OVS + GRE 之 Compute node 篇

    0.环境 硬件环境见上一篇博客:学习OpenStack之(5):在Mac上部署Juno版本OpenStack 四节点环境 OpenStack网络配置:一个tenant, 2个虚机 Type drive ...

  4. openstack-lanch an instance and nova compute log analysis

    1. how to launch an instance: [root@localhost ~(keystone_admin)]# nova flavor-list+----+-----------+ ...

  5. 【原创翻译】初识Unity中的Compute Shader

    一直以来都想试着自己翻译一些东西,现在发现翻译真的很不容易,如果你直接把作者的原文按照英文的思维翻译过来,你会发现中国人读起来很是别扭,但是如果你想完全利用中国人的语言方式来翻译,又怕自己理解的不到位 ...

  6. Many2one类型的fields Compute得到的值 搜索

    v8 默认情况下compute的值不存储于数据库中,在高级搜索中也不可以进行搜索 想要对这种类型的值搜索,需要在field的定义中添加search参数,在search的函数中编写搜索逻辑. 例子: r ...

  7. A trip through the Graphics Pipeline 2011_13 Compute Shaders, UAV, atomic, structured buffer

    Welcome back to what’s going to be the last “official” part of this series – I’ll do more GPU-relate ...

  8. DataTable.Compute()用法

    DataTable.Compute()用法 2010-04-07 11:28 一.DataTable.Compute()方法說明如下 作用:          计算用来传递筛选条件的当前行上的给定表达 ...

  9. 转:DataTable的Compute方法的应用

    转自:http://www.cnblogs.com/hfliyi/archive/2013/01/08/2851944.html 项目中遇到计算平均值.标准偏差.平均值+标准偏差.平均值+2倍标准偏差 ...

随机推荐

  1. Flutter 基础组件:图片和Icon

    前言 Flutter中,可以通过Image组件来加载并显示图片,Image的数据源可以是asset.文件.内存以及网络. ImageProvider 是一个抽象类,主要定义了图片数据获取的接口load ...

  2. 天梯赛练习 L3-008 喊山 (30分) bfs搜索

    题目分析: 本题是一题比较简单的bfs搜索题,首先由于数据给的比较多不能直接开二维数组存放,而是用了vector的动态的二维数组的形式存放,对于每个出发点,我们bfs向四周搜索,标记搜索过的点,遇到搜 ...

  3. Deep Learn I'm back.

    Intorduction: 时隔好几个月,我准备重新进入Deep Learning 的领域.昨天和老师聊了很多,之前觉得我做的工作就是排列组合,在水论文,灌水.但老师却说:这也是为将来的研究打基础. ...

  4. maven依赖与传递性依赖

    目录 依赖范围 传递性依赖 依赖调节 可选依赖 本文主要是针对<maven实战>书中关键知识点的学习记录,未免有纰漏或描述不到之处,建议购买阅读原书 首先贴出一个pom常见的一些元素释义 ...

  5. 【UML】基本介绍与类图(依赖、泛化、实现、关联、聚合、组合关系)

    文章目录 UML基本介绍 UML图 UML类图 类图-依赖关系(Dependence) 类图-泛化关系(generalization) 类图-实现关系(Implementation) 类图-关联关系( ...

  6. MySQL全面瓦解18:自定义函数

    定义 我们之前学习了MySQL的内置函数,非常丰富,满足了我们对数据操作的大部分需求. 但是如果有一些复杂的业务逻辑在数据库层面就可以完成,无需在程序层面完成的时候,这时候就可以写成MySQL自定义函 ...

  7. layui表格数据统计

    //执行一个 table 实例 table.render({ elem: '#demo' ,height: 420 ,url: '/demo/table/user/' //数据接口 ,title: ' ...

  8. Nginx(七):location的使用以及nginx关闭原理

    上一篇中,我们了解了如何nginx的配置原则及解析框架,以及解析location配置的具体实现,相信大家对该部分已经有了比较深刻的认识. 本篇,我们进一步来了解下,解析之后的配置,如何应用到实际中的吧 ...

  9. Jmeter如何录制APP客户端脚本

    简单五步教大家Jmeter录制APP客户端脚本: Step1 右键单击该测试计划,选择"添加"-"线程组",添加一个线程组. Step2 为了录制客户端的操作, ...

  10. ospf-lite runs over tcp / udp port 8899.

    draft-thomas-reed-ospf-lite-01 - ospf-lite https://tools.ietf.org/html/draft-thomas-reed-ospf-lite-0 ...