码迷,mamicode.com
首页 > 其他好文 > 详细

hdu2196:树形dp

时间:2015-12-15 22:55:24      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:

其实很简单,先计算子树的max,再计算父树的max就OK了;

-----------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))


int read(){
 int x=0; int f=1;
 char c=getchar();
 while(!isdigit(c)){
  if(c==‘-‘) f=-1;
  c=getchar();
 }
 while(isdigit(c)){
  x=x*10+c-‘0‘;
  c=getchar();
 }
 return x*f;
}


struct edge{
 int to,w;
 edge*next;
};
edge e[10005],*pt,*head[10005];int f[10005][3];


void add(int s,int t,int w){
 pt->to=t; pt->w=w;
 pt->next=head[s];
 head[s]=pt++;
}
void dp1(int x){
 for(edge*ee=head[x];ee;ee=ee->next){
  int to=ee->to; int w=ee->w;
  dp1(to);
  if(f[to][1]+w>f[x][1]){
   f[x][0]=f[x][1];f[x][1]=f[to][1]+w;
  }
  else if(f[to][1]+w>f[x][0]){
   f[x][0]=f[to][1]+w;
  }
 }
 return ;
}
void dp2(int x,int fa,int len){
 if(f[x][1]+len==f[fa][1]){
  f[x][2]=max(f[fa][2],f[fa][0]);
 }
 else{
  f[x][2]=max(f[fa][2],f[fa][1]);
 }
 f[x][2]+=len;
 for(edge*ee=head[x];ee;ee=ee->next){
  int to=ee->to;int w=ee->w;
  dp2(to,x,w);
 }
 return ;
}
int main(){
 int n;
 while(scanf("%d",&n)==1){
  clr(f,0);clr(head,0);
  pt=e;
  for(int i=2;i<=n;i++){
   int u=read(),v=read();
   add(u,i,v);
  }
  /*rep(i,n){
      cout<<i<<endl;
      for(edge*ee=head[i];ee;ee=ee->next)
       printf("%d %d\n",ee->to,ee->w); 

 }*/
  dp1(1);
  dp2(1,0,0);
  rep(i,n)   printf("%d\n",max(f[i][1],f[i][2]));
 }
 return 0;
}

 

-----------------------------------------------------------------------------------

 

 

Computer

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


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   |   We have carefully selected several similar problems for you:  1561 1011 3456 2242 2602 

 

 

hdu2196:树形dp

标签:

原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5049554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!