题目链接:

Computer

Time Limit: 1000/1000 MS (Java/Others)   

 Memory Limit: 32768/32768 K (Java/Others)

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找到一个端点;再来一遍bfs找到所有点到这个端点的距离,这些距离可能就是最后的答案,也可能不是,然后再从直径的另一个端点出发,找到所有点到这个端点的距离,和上个距离取最大就是结果了;
 
AC代码:
/*    2196    15MS    1976K    2205 B    G++    2014300227*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+;
typedef long long ll;
const double PI=acos(-1.0);
int n,vis[N],head[N],cnt,a,b,dp[N],fp[N],mmax,ending;
queue<int>qu;
struct Edge
{
int to,next,val;
};
Edge edge[*N];
void add_edge(int s,int e,int va)
{
edge[cnt].to=e;
edge[cnt].next=head[s];
edge[cnt].val=va;
head[s]=cnt++;
}
void dfs(int x,int leng)
{
if(leng>mmax)
{
ending=x;
mmax=leng;
}
vis[x]=;
for(int i=head[x];i!=-;i=edge[i].next)
{
int y=edge[i].to;
if(!vis[y])
{
dfs(y,leng+edge[i].val);
}
}
}
void bfs1()
{
qu.push(ending);
dp[ending]=;
vis[ending]=;
while(!qu.empty())
{
int fr=qu.front();
qu.pop();
for(int i=head[fr];i!=-;i=edge[i].next)
{
int y=edge[i].to;
if(vis[y])
{
dp[y]=dp[fr]+edge[i].val;
qu.push(y);
vis[y]=;
}
}
}
}
void bfs2()
{
int start,mmx=;
for(int i=;i<=n;i++)
{
if(dp[i]>mmx)
{
start=i;
mmx=dp[i];
}
}
qu.push(start);
fp[start]=;
vis[start]=;
while(!qu.empty())
{
int fr=qu.front();
qu.pop();
for(int i=head[fr];i!=-;i=edge[i].next)
{
int y=edge[i].to;
if(!vis[y])
{
fp[y]=fp[fr]+edge[i].val;
dp[y]=max(dp[y],fp[y]);
vis[y]=;
qu.push(y);
}
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
{
vis[i]=;
head[i]=-;
}
cnt=;
mmax=;
for(int i=;i<=n;i++)
{
scanf("%d%d",&a,&b);
add_edge(i,a,b);
add_edge(a,i,b);
}
dfs(,);
bfs1();
bfs2();
for(int i=;i<=n;i++)
{
printf("%d\n",dp[i]);
}
}
return ;
}
 
 

hdu-2169 Computer(树形dp+树的直径)的更多相关文章

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

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

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

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

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

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

  4. hdu 4607 树形dp 树的直径

    题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n)个点,至少需要走多少距离(每条边的距离是1): 思路:树形dp求树的直径r: a:若k<=r+1 ...

  5. HDU 2196 Computer 树形DP经典题

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

  6. VIJOS1476旅游规划[树形DP 树的直径]

    描述 W市的交通规划出现了重大问题,市政府下决心在全市的各大交通路口安排交通疏导员来疏导密集的车流.但由于人员不足,W市市长决定只在最需要安排人员的路口安放人员.具体说来,W市的交通网络十分简单,它包 ...

  7. HDU 2196 Computer 树形DP 经典题

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

  8. POJ 3162.Walking Race 树形dp 树的直径

    Walking Race Time Limit: 10000MS   Memory Limit: 131072K Total Submissions: 4123   Accepted: 1029 Ca ...

  9. poj3162 树形dp|树的直径 + 双单调队列|线段树,好题啊

    题解链接:https://blog.csdn.net/shiqi_614/article/details/8105149 用树形dp是超时的,, /* 先求出每个点可以跑的最长距离dp[i][0|1] ...

随机推荐

  1. vs调试,监控变量

  2. 阿里云官方教程 Linux 系统挂载数据盘

    适用系统:Linux(Redhat , CentOS,Debian,Ubuntu) *  Linux的云服务器数据盘未做分区和格式化,可以根据以下步骤进行分区以及格式化操作. 下面的操作将会把数据盘划 ...

  3. DisplayPort的时钟隐藏和时钟恢复

    转:DisplayPort的时钟隐藏和时钟恢复 无时钟线的视频数据传输是DP协议的一大特点,将时钟信号隐藏在数据中是传输协议的设计趋势.时钟恢复技术也是DP芯片设计的关键技术.在这说一下在发送端时钟是 ...

  4. jquery文档处理如after错误

    $('.foo').after('<li>0<li>')会添加两个元素 $('.foo').after('<li>o</li>')只会添加一个元素 所以 ...

  5. iBatis2 SqlMap中经常使用sql语句

    本来我也不喜欢iBatis,那是由于我当时还不怎么会用它,如今我想说,iBatis是个好东西,不信你试试看.以下是我在项目实践中对iBatis的一个小总结.希望帮助众多在疲于iBatis编码而无暇思考 ...

  6. HTML5 2D平台游戏开发#4状态机

    在实现了<HTML5 2D平台游戏开发——角色动作篇之冲刺>之后,我发现随着角色动作的增加,代码中的逻辑判断越来越多,铺天盖地的if() else()语句实在让我捉襟见肘: 这还仅仅是角色 ...

  7. Paxos算法学习

    早在1990年,Leslie Lamport(即 LaTeX 中的"La",微软研究院科学家,获得2013年图灵奖)向ACM Transactions on Computer Sy ...

  8. php正则表达式匹配img中任意属性的方法

    经常和图片打交道,不得不用到一些提取图片中scr.alt.title.等的属性,这里总结给大家一些常用的,感觉还不错,比较通用! PHP正则表达式匹配img中任意属性PHP 复制代码代码如下: < ...

  9. PHP面向对象——构造函数、析构函数

    在PHP中构造函数和析构函数是固定的,如下: // 构造函数 function __construct([argument1,argument2,...,argumentN]){ /* Class i ...

  10. 【Python基础】之异常

    一.常见异常 try: open('abc.txt','r') except FileNotFoundError: print('异常啦!') 输出结果: ======= 异常啦! 我们通过 open ...