HDU 2196 树状dp 求树中节点之间的最长距离

Computer

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

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

HDU 2196:

题目意思:

求一棵树中的任意一个点到树中其他的节点的最长距离;

解题思路:

两次dp,第一次dp的时候求出以点i为根的子树中i到其他节点的最长距离,状态转移方程为:

dp[i]=max(dp[son]+w[i][son],dp[i]);

注:在第一次运算的时候,需要把最大值和次大值都记录下来;

第二次dp的时候算从i节点的父亲节点过来的最大值,如果i节点的父亲节点的最大值是从i节点进行状态转移转化过去的,则需要用此时的最大值与父亲节点的次大值加上边权值与原来的最大值比较;

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<vector>
 7 using namespace std;
 8 const int maxn=10007;
 9 int dp[maxn][3];
10 struct node
11 {
12     int to,cost;
13 };
14 vector<node> G[maxn];
15
16 int n;
17 void init()
18 {
19     memset(dp,0,sizeof(dp));
20     for(int i=0;i<maxn;i++)
21         G[i].clear();
22 }
23 void slove1(int point,int father)
24 {
25     int big=0,bigger=0;
26     for(int i=0;i<G[point].size();i++)
27     {
28         int v=G[point][i].to;
29         if(v==father) continue;
30         slove1(v,point);
31         if(dp[v][0]+G[point][i].cost>big)
32         {
33             bigger=big;
34             big=dp[v][0]+G[point][i].cost;
35         }
36         else if(dp[v][0]+G[point][i].cost>bigger) bigger=dp[v][0]+G[point][i].cost;
37     }
38     dp[point][0]=big;
39     dp[point][1]=bigger;
40 }
41 void slove2(int point,int father)
42 {
43     int t;
44     for(int i=0;i<G[point].size();i++)
45     {
46         int v=G[point][i].to;
47         if(v==father) continue;
48         if(dp[point][0]==dp[v][0]+G[point][i].cost) t=dp[point][1];
49         else t=dp[point][0];
50         dp[v][2]=max(dp[point][2],t)+G[point][i].cost;
51         slove2(v,point);
52     }
53 }
54 int main()
55 {
56  // freopen("in.txt","r",stdin);
57     int a,b;
58      while(~scanf("%d",&n)){
59         init();node n1;
60         for(int i=2;i<=n;i++)
61         {
62             scanf("%d %d",&a,&b);
63             n1.to=a;n1.cost=b;
64             G[i].push_back(n1);
65             n1.to=i;
66             G[a].push_back(n1);
67         }
68     slove1(1,-1);
69     dp[1][2]=0;
70     slove2(1,-1);
71     for(int i=1;i<=n;i++)
72         printf("%d\n",max(dp[i][2],dp[i][0]));
73      }
74     return 0;
75 }
时间: 2024-10-12 17:26:10

HDU 2196 树状dp 求树中节点之间的最长距离的相关文章

HDU 3586 树状dp

Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 1668    Accepted Submission(s): 618 Problem Description In the battlefield , an effective way to defeat enemies is to brea

hdu 1754 树状数组求最大值

#include <stdio.h> #include <string> #define MAX(a,b) (a>b?a:b) #define Lowbit(x) (x & (-x)) int idx[200010], num[200010]; /*int Lowbit(int x) { return x&(-x); }*/ /*int MAX(int x, int y) { return x > y ? x:y; }*/ /* 对于区间 [l,r] 把

HDU 1394 树状数组求逆序对

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13036    Accepted Submission(s): 7968 Problem Description The inversion number of a given number sequence a1, a2, ..., an

HDU 4035Maze(树状+概率dp,绝对经典)

题意: 给你n个节点的树,从1节点开始走,到每个节点都有三种情况,被杀死回到1节点,找到隐藏的出口出去,沿着当前节点相邻的边走到下一个节点,给出每个节点三种情况发生的概率分别为ki,ei,1-ki-ei,求找到出口时已经过的边数的期望. 分析: 用树状dp考虑问题.当节点是叶子节点时它只是向父节点走,非叶子节点可以向父亲节点和所有孩子节点走. Ei表示到i节点经过边数的期望 则叶子节点:Ei=ki*E1+(1-ki-ei)*(E[par[i]]+1);//par[i]表示i的父亲节点 非叶子节点

HDU 1394 Minimum Inversion Number (树状数组求逆序数)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13942    Accepted Submission(s): 8514 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m = 0 - the initial seqence)a2, a3, ..., an, a1 (where m = 1)a3, a4, ..., an, a1, a2 (where m = 2)...an, a1, a2, ..., an-1 (where m = n-1)求这些序列中,逆序数最少的

HDU 1394 Minimum Inversion Number (树状数组求逆序对)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多少. 一开始就求原来初始数组的逆序对,树状数组求或者归并方法求(可以看<挑战程序设计>P178),然后根据最前面的元素大小递推一下每次移到最后得到的逆序数,取最小值. 1 #include <iostream> 2 #include <cstdio> 3 #include

HDU 5249 离线树状数组求第k大+离散化

KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1160    Accepted Submission(s): 488 Problem Description 你工作以后, KPI 就是你的全部了. 我开发了一个服务,取得了很大的知名度.数十亿的请求被推到一个大管道后同时服务从管头拉取请求.让我们来定义每个请求都有一个重要值.我的

hdu 5147 Sequence II (树状数组 求逆序数)

题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 331    Accepted Submission(s): 151 Problem Description Long long ago, there is a sequence A with length n. All numbers in this se