Computer

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

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
题意:给你一棵树,求每个节点的最远对点的距离。
题目看似有点难度,但我们想一想两遍dfs求树的直径的过程,第一遍dfs找的是随意一个点的最远对点,它一定是树的直径的端点,那么我们就可以猜想一下是不是每个点的最远对点都是树的直径的端点呢?答案是肯定的。那么我们就可以先把树的直径找出来然后分别求两个端点到所有的点的距离,取最大值即可。
代码如下:
 #include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#define maxn 10005
using namespace std; struct edge
{
int next;
int to;
int dis;
}g[maxn<<]; inline int read()
{
char c=getchar();
int res=,x=;
while(c<''||c>'')
{
if(c=='-')
x=-;
c=getchar();
}
while(c>=''&&c<='')
{
res=res*+(c-'');
c=getchar();
}
return x*res;
} int n,aa,bb,num,root,ans;
int last[maxn],d[maxn],dp[maxn],dp1[maxn]; inline void add(int from,int to,int dis)
{
g[++num].next=last[from];
g[num].to=to;
g[num].dis=dis;
last[from]=num;
} void dfs(int x)
{
d[x]=;
for(int i=last[x];i;i=g[i].next)
{
int v=g[i].to;
if(!d[v])
{
dp[v]=dp[x]+g[i].dis;
dfs(v);
}
}
} void dfs1(int x)
{
d[x]=;
for(int i=last[x];i;i=g[i].next)
{
int v=g[i].to;
if(!d[v])
{
dp[v]=dp[x]+g[i].dis;
dfs1(v);
}
}
} void dfs2(int x)
{
d[x]=;
for(int i=last[x];i;i=g[i].next)
{
int v=g[i].to;
if(!d[v])
{
dp1[v]=dp1[x]+g[i].dis;
dfs2(v);
}
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
ans=;num=;
memset(last,,sizeof(last));
memset(dp,,sizeof(dp));
memset(d,,sizeof(d));
memset(dp1,,sizeof(dp1));
for(int i=;i<=n;i++)
{
aa=read();bb=read();
add(i,aa,bb);
add(aa,i,bb);
}
dfs();
for(int i=;i<=n;i++)
{
if(dp[i]>ans)
{
ans=dp[i];
root=i;
}
}
memset(dp,,sizeof(dp));
memset(d,,sizeof(d));
dfs1(root);
ans=;
memset(d,,sizeof(d));
for(int i=;i<=n;i++)
{
if(dp[i]>ans)
{
ans=dp[i];
root=i;
}
}
dfs2(root);
for(int i=;i<=n;i++)
{
printf("%d\n",max(dp[i],dp1[i]));
}
}
return ;
}

Don't waste your time on a man/woman, who isn't willing to waste their time on you.
不要为那些不愿在你身上花费时间的人而浪费你的时间

--snowy

2019-01-18  07:49:19

HDU 2196 Compute --树形dp的更多相关文章

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

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

  2. HDU 2196 Computer 树形DP经典题

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

  3. HDU 2196 Computer 树形DP 经典题

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

  4. HDU - 2196(树形DP)

    题目: A school bought the first computer some time ago(so this computer's id is 1). During the recent ...

  5. hdu 2196【树形dp】

    http://acm.hdu.edu.cn/showproblem.php?pid=2196 题意:找出树中每个节点到其它点的最远距离. 题解: 首先这是一棵树,对于节点v来说,它到达其它点的最远距离 ...

  6. hdu 2196 Computer(树形DP)

    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经典)

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

  9. HDU 2196 Computer (树dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...

随机推荐

  1. 控制结构(4): 局部化(localization)

    // 上一篇:状态机(state machine) // 下一篇:必经之地(using) 基于语言提供的基本控制结构,更好地组织和表达程序,需要良好的控制结构. 前情回顾 上一次,我们说到状态机结构( ...

  2. nginx(一)初识nginx

    什么是nginx?Nginx (engine x) 是一款轻量级的Web 服务器 .反向代理服务器及电子邮件(IMAP/POP3)代理服务器. Nginx应用场景(都很常用): 1:http服务器.N ...

  3. Q&A in Power BI service and Power BI Desktop

    What is Q&A? Sometimes the fastest way to get an answer from your data is to ask a question usin ...

  4. 利用window.performance.timing进行性能分析

    性能分析... window.performance.timing中相关属性语义: // .navigationStart 准备加载页面的起始时间 // .unloadEventStart 如果前一个 ...

  5. 关于rem布局

    实际UI设计稿给过来为了在手机屏幕上显示清晰,设计稿通常为实际标准手机页面的2倍,一般为640px(以ip5的屏幕尺寸320px设计)或者750px(以ip6的屏幕尺寸为375px设计),这是前提. ...

  6. Django ORM 操作2 增删改

    增删改 增加 表对象直接增加方式 Frank_obj = models.Student(name ="海东",course="python",birth=&qu ...

  7. opencv 增强现实(一):特征点检测

    import cv2 as cv import numpy as np def draw_keypoints(img, keypoints): for kp in keypoints: x, y = ...

  8. 了解PID控制

    @2019-03-07 [小记] 了解PID控制 比例 - 积分 - 微分 积分 --- 记忆过去 比例 --- 了解现在 微分 --- 预测未来

  9. Shell入门及实践

    解释器 解释器是一种命令解释器,主要作用是对命令进行运行和解释,将需要执行的操作传递给操作系统内核并执行 #!/bin/bash(默认),指定解释器 #!/bin/bash #这是第一个shell脚本 ...

  10. 解决Java getResource 路径中含有中文的情况

    问题描述 当Java调用getResource方法,但是因为路径中含有中文时,得不到正确的路径 问题分析 编码转换问题 当我们使用ClassLoader的getResource方法获取路径时,获取到的 ...