题目链接:

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. 微信开源组件WCDB漫谈及Demo

    代码地址如下:http://www.demodashi.com/demo/12422.html 前言 移动端的数据库选型一直是一个难题,直到前段时间看到了WeMobileDev(微信前端团队)放出了第 ...

  2. VueJS事件处理器v-on

    事件监听可以使用 v-on 指令. v-on:click表达式 HTML: <!DOCTYPE html> <html> <head> <meta chars ...

  3. phpQuery—基于jQuery的PHP实现(转)

    Query的选择器之强大是有目共睹的,phpQuery 让php也拥有了这样的能力,它就相当于服务端的jQuery. 先来看看官方简介: phpQuery is a server-side, chai ...

  4. 线性表的链式实现(C++)

    相关内容: 线性表的顺序实现 链式实现(C语言) (推荐在GitHub上查看,下面代码只是最初实现,后续如果有修改,由于太麻烦,不会再更改下文,仅仅更新Github上的代码文件) 结点以及链表类的定义 ...

  5. Java结束线程的三种方法

    线程属于一次性消耗品,在执行完run()方法之后线程便会正常结束了,线程结束后便会销毁,不能再次start,只能重新建立新的线程对象,但有时run()方法是永远不会结束的.例如在程序中使用线程进行So ...

  6. Ejabberd作为推送服务的优化手段(转)

    AVOS Cloud目前还在用Ejabberd做Android的消息推送服务.当时选择Ejabberd,是因为Ejabberd是一个发展很长时间的XMPP实现,并且基于Erlang,设想能在我们自主研 ...

  7. 01 http协议概念及工作流程

    一:HTTP协议 重要性: 无论是以后用webserverice ,还是用rest做大型架构,都离不开对HTTP协议的认识. 甚至可以简化的说: webservice = http协议+XML Res ...

  8. Java学习之路 第四篇 oop和class (面向对象和类)

    本人水平有限,创作本文是为了记录学习和帮助初学者学习,欢迎指正和补充 一.面向对象编程的设计概述 很多同学都在学校学了电脑的编程,现在的书籍大部分都是oop面向对象编程,一个很抽象的的名字,比较难以理 ...

  9. linux环境tomcat配置及hadoop 2.6伪分布模式安装配置

    一.ubuntu 15.04.openjdk1.7.tomcat7环境配置 1. 配置openjdk1.7,输入命令: -jdk 2. 查看java是否安装成功,输入命令: envjava -vers ...

  10. 图像处理之canny---求梯度

    梯度求法和sobel之类的算子雷同,甚至更简单,就是一个离散差分,不清楚的童鞋可以百度,一大堆资料呢,从源码也可清晰的看出原理. // 方向导数,求梯度/* * @parameter sz: 图像大小 ...