Computer

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

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

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#define N 10010
using namespace std;
struct node
{
int to,len;//下一个节点,长度
node (int x,int y)
{
to=x;
len=y;
}
};
int n;
/*
第一个dfs是先搜然后再转移状态的,用结点下方的数据更新
第二个dfs是先状态转移然后再搜的,这是从结点上方进行数据更新,刚好满足题意:结点左右两个“子树”
*/
vector<node> adj[N*];
int maxn[N];//第i个点的最大权值
int smaxn[N];//第i个点的第二大的权值
int maxi[N];//最大权值的点
int smaxi[N];//第二大权值的点
void dfs1(int u,int p)//p是u的父节点
{
maxn[u]=;
smaxn[u]=;
for(int i=;i<adj[u].size();i++)
{
int v=adj[u][i].to;
if(v==p) continue;//和父节点一样
dfs1(v,u);
if(maxn[v]+adj[u][i].len>smaxn[u])//先和第二长的距离比较,这样能记录下第二长的距离,这个数据能为下一个dfs提供判断
{
smaxn[u]=maxn[v]+adj[u][i].len;
smaxi[u]=v;
if(maxn[u]<smaxn[u])//更新之后第二长的路径,如果比原来最长的路径还长就替换,并且原来的最长的就变成第二长得了
{
swap(maxn[u],smaxn[u]);
swap(maxi[u],smaxi[u]);
}
}
}
} //从父节点更新过来的
void dfs2(int u,int p)
{
for(int i=;i<adj[u].size();i++)
{
int v=adj[u][i].to;
if(v==p) continue;//和父节点一样
if(v==maxi[u])
//这个地方如果下一个结点刚巧是就是dfs1中搜出来的最大值的节点的话
//那么这一条路就不能用最大值去算,因为这样就算上上一条路的了
{
if(adj[u][i].len+smaxn[u]>smaxn[v])
{
smaxn[v]=adj[u][i].len+smaxn[u];
smaxi[v]=u;
if(maxn[v]<smaxn[v])
{
swap(maxn[v],smaxn[v]);
swap(maxi[v],smaxi[v]);
}
}
}
else
{
if(adj[u][i].len+maxn[u]>smaxn[v])
{
smaxn[v]=adj[u][i].len+maxn[u];
smaxi[v]=u;
if(maxn[v]<smaxn[v])
{
swap(maxn[v],smaxn[v]);
swap(maxi[v],smaxi[v]);
}
}
}
dfs2(v,u);
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
adj[i].clear();
for(int i=;i<=n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
adj[i].push_back(node(a,b));
adj[a].push_back(node(i,b));
}
dfs1(,-);//从子节点更新
dfs2(,-);//从父节点更新
for(int i=;i<=n;i++)
printf("%d\n",maxn[i]);
}
return ;
}

hdu 2196 Computer(树形DP经典)的更多相关文章

  1. HDU 2196 Computer 树形DP经典题

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

  2. HDU 2196 Computer 树形DP 经典题

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

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

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

  4. hdu 2196 Computer(树形DP)

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

  5. hdu 2196 Computer 树形dp模板题

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

  6. HDU 2196 Computer (树dp)

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

  7. HDU - 2196(树形DP)

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

  8. hdu 2196【树形dp】

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

  9. HDU 2196 Compute --树形dp

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

随机推荐

  1. 【JVM命令系列】jstack

    jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项"-J-d64",Windows的jstack使 ...

  2. 接口测试——HttpClient工具的https请求、代理设置、请求头设置、获取状态码和响应头

    目录 https请求 代理设置 请求头设置 获取状态码 接收响应头 https请求 https协议(Secure Hypertext Transfer Protocol) : 安全超文本传输协议, H ...

  3. JAVAWEB复习资料-01

    CSS中@import和link两种插入样式表方式有什么不同? 1.link属于HTML标签,除了引入css文件之外还能定义RSS等,而@import只能用于加载CSS. 2.link在引用CSS时, ...

  4. mysql error 1290 hy000:The MySQL server is running with the --skip-grant-tables option so it cannot execute this statemen' 解决方案

    如果在执行授权命令的时候报错 mysql> grant all privileges on *.* to root@"; ERROR (HY000): The MySQL server ...

  5. web应用程序 前段部分调优

    1. 使用瀑布图初步诊断网站性能瓶颈 一般来说,打开一个网页的速度会受到以下几项的影响: 1) 服务器花了太长的时间将.aspx页面的内容转化为html. 2) .aspx页面花了太长的时间从服务器端 ...

  6. jquery定时刷新数据

    $(function () { setInterval("startRequest()", 3000); }); function startRequest() { $(" ...

  7. PHP二维关联数组的遍历方式

    采用foreach循环对二维索引数组进行遍历,相对来讲速度更快,效率更高,foreach循环是PHP中专门用来循环数组的.实例也相对简单,多加练习,想清楚程序运行逻辑即可. <?php $arr ...

  8. win10 uwp 上传Nuget 让别人用我们的库

    Nuget 我们的开发经常使用别人的dll,那么我们需要每次都从网上下载,然后复制到我们的项目, 而不知道我们的dll是否安全? 当我们的库更新的时候,我们又需要从网上搜索,这样不好,于是我们就用Nu ...

  9. 简述Apache的ab测试主要有那些关键指标

    一.ab的原理 ab是apachebench命令的缩写. ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问.它的测试目标是基于URL的,因此,它既可以用来测试ap ...

  10. 安装debian 9.1后,中文环境下将home目录下文件夹改为对应的英文

    安装了debian 9.1后,中文环境下home目录下文件夹显示的是中文,相当不方便cd命令,改为对应的英文吧,需要用到的软件xdg-user-dirs-gtk #安装需要的软件 sudo apt i ...