Computer

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

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
 
题意:求一棵树上任一点到其他点的最远距离。
思路:1)树上的任意一点到其他点的最远距离必然是到达树的直径的两个端点之一,树的直径只需要2遍dfs即可。
2)重点说一下树形dp的思路。dp[i][0],dp[i][1],dp[i][2]分别表示:i通过子树可以达到的最远距离;i通过子树可以到达的次远距离(不同儿子);i通过父亲能够到达的最远距离。那么ans[i]=max(dp[i][0],dp[i][2]);
对于<u,v>w:
状态转移方程:dp[u][0]=dp[v][0]+w;dp[u][1]也是这样得到,但是注意dp[i][1]是通过另外的儿子获得。
1.dp[v][2]=dp[u][1]+w; 2.dp[v][2]=dp[u][0]+w;当<u,v> 是u的最长子树的路径时,只能用1式子。
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<stack>
#include<set>
#include<bitset>
using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int > P;
const int N=1e5+,M=1e5+;
const int inf=0x3f3f3f3f;
const ll INF=1e18+,mod=1e9+;
struct edge
{
int from,to;
ll w;
int next;
};
edge es[M];
int cnt,head[N];
ll dp[N][];
void init()
{
cnt=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,ll w)
{
cnt++;
es[cnt].from=u,es[cnt].to=v;
es[cnt].w=w;
es[cnt].next=head[u];
head[u]=cnt;
}
void dfs1(int u,int fa)
{
for(int i=head[u]; i!=-; i=es[i].next)
{
edge e=es[i];
if(e.to==fa) continue;
dfs1(e.to,u);
ll d=dp[e.to][]+e.w;
if(d>dp[u][]) swap(d,dp[u][]);
if(d>dp[u][]) swap(d,dp[u][]);
}
}
void dfs2(int u,int fa)
{
for(int i=head[u]; i!=-; i=es[i].next)
{
edge e=es[i];
if(e.to==fa) continue;
if(dp[u][]==dp[e.to][]+e.w)
dp[e.to][]=max(dp[u][],dp[u][])+e.w;
else
dp[e.to][]=max(dp[u][],dp[u][])+e.w;
dfs2(e.to,u);
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
init();
for(int i=,j; i<=n; i++)
{
ll w;
scanf("%d%lld",&j,&w);
addedge(i,j,w);
addedge(j,i,w);
}
memset(dp,,sizeof(dp));
dfs1(,);
dfs2(,);
for(int i=; i<=n; i++)
printf("%d\n",max(dp[i][],dp[i][]));
}
return ;
}

树形dp

HDU 2196.Computer 树形dp 树的直径的更多相关文章

  1. HDU 2196 Computer 树形DP经典题

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

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

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

  3. HDU 2196 Computer 树形DP 经典题

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

  4. hdu-2169 Computer(树形dp+树的直径)

    题目链接: Computer Time Limit: 1000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) ...

  5. hdu 2196 Computer(树形DP)

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

  6. hdu 2196 Computer 树形dp模板题

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

  7. hdu 2196 Computer(树形DP经典)

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

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

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

  9. hdu 4607 树形dp 树的直径

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

随机推荐

  1. Linux 修改zabbix server的web访问端口

    在安装zabbix server的时候默认就安装了apache,zabbix依靠apache提供的web服务,修改Zabbix的浏览器访问端口,就是修改apache的服务端口(默认端口:80) 1.编 ...

  2. leetcode每日刷题计划-简单篇day6

    突发奇想&胡思乱想的一天 银行家算法证明错了并挂在黑板上的可怜希希 Num 53 最大子序和 Maximum Subarray O(n)的算法实现了,分治法有空补 class Solution ...

  3. 基于SSM的CRUD项目的详解

    创一个maven工程 创建web的目录和web.xml------------右击项目,build projet--->configure  project---->Project fac ...

  4. Django上传文件和上传图片(不刷新页面)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. python docker 多进程提供 稳定tensorflow gpu 线上服务

    尝试了太多的python多进程的服务,在tensorflow 的线上GPU服务中总是不理想.tensorlfow serving docker服务这些也有些不便. 今天抽空给大家分享一个成功的经验.失 ...

  6. JavaScript值全等判断

    作为开发员,很多时候拿到数据之后都是要做数据判断,比较特别的情况就是我们需要做数组判断和对象判断,经常的我们就array === array ,object === object;但是可惜是我们得到的 ...

  7. react项目如何修改默认3000端口号

    在运行react项目时,经常会遇到默认的3000端口被占用的情况,此时不想查找哪个程序占用了3000端口,想使用其他端口继续运行. 打开项目中的node_modules文件夹,找到react_scri ...

  8. 跑步“无核心,不PB”

    核心力量不管是在跑步中,还是在生活中都有着重要的作用,核心能让你的身体机能更加强劲. 1.什么是核心肌群? 核心肌群就是指我们所说的躯干,包括脊柱.骨盆.及周围肌群. 核心肌群由腹直肌.腹横肌.腹斜肌 ...

  9. Homework:奇偶性

    // 程序功能: // 要求用户从键盘输入一个整数,判断其是奇数还是偶数 #include <stdio.h> int main() { int x; printf("输入一个整 ...

  10. 认识border

    标签(空格分隔): border border的认识: border:边框的意思,描述盒子的边框,边框有三个要素: 粗细, 线性样式 ,颜色: <!DOCTYPE html> <ht ...