hdu 2196 Computer 树形DP 求每个点的最远点距离 两次dfs的经典树形dp

来源:互联网 发布:js eval函数缺少) 编辑:程序博客网 时间:2024/05/29 04:26

Computer

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


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
51 12 13 11 1
 

Sample Output
32344
 

Author
scnu
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1561 1011 3456 2242 2602 
 


题意:给出一棵边带权的树,求出每个点的最远点距离。


最新代码:2016/10/25

#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>using namespace std;#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])#define mem(a,x)  memset(a,x,sizeof a)#define ysk(x)  (1<<(x))const int maxn=10000    ;int N,fir[maxn+5];int nedge,nex[2*maxn+10],dp_far[maxn+10][2];struct Edge{    int to,w;}e[2*maxn+10];void add_edge(int x,int y,int w){    e[nedge].to=y;    e[nedge].w=w;    nex[nedge]=fir[x];    fir[x]=nedge++;}void insert(int val,int &x0,int &x1){    if(val>x0)    {        x1=x0;        x0=val;    }    else  if(val>x1)    {        x1=val;    }}void dfs(int x,int fa){    dp_far[x][0]=dp_far[x][1]=0;    for(int i=fir[x];~i;i=nex[i])    {        int y=e[i].to;if(y==fa)  continue;        int w=e[i].w;        dfs(y,x);        insert(dp_far[y][0]+w,dp_far[x][0],dp_far[x][1]);    }}void dfs2(int x,int fa){    for(int i=fir[x];~i;i=nex[i])    {        int y=e[i].to;if(y==fa)  continue;        int w=e[i].w;        if(dp_far[y][0]+w==dp_far[x][0]) insert(dp_far[x][1]+w,dp_far[y][0],dp_far[y][1]);        else   insert(dp_far[x][0]+w,dp_far[y][0],dp_far[y][1]);        dfs2(y,x);    }}int main(){   std::ios::sync_with_stdio(false);   int x,w;   while(cin>>N)   {       nedge=0;mes(fir,-1,N+1);       for(int i=2;i<=N;i++)       {           cin>>x>>w;           add_edge(i,x,w);           add_edge(x,i,w);       }       dfs(1,-1);       dfs2(1,-1);       for1(i,N)  printf("%d\n",dp_far[i][0]);   }   return 0;}




老代码:

#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<cctype>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI (4.0*atan(1.0))#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)#define  lson   ind<<1,le,mid#define rson    ind<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mk    make_pair#define _f     first#define _s     second#define ysk(x)  (1<<(x))using namespace std;//const int INF=    ;typedef long long ll;//const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);const int INF =0x3f3f3f3f;const int maxn=10000+20    ;//const int maxm=    ;struct Edge{    int from,to,w;    Edge(){}    Edge(int from,int to,int w):from(from),to(to),w(w){}};vector<Edge>edges;vector<int >G[maxn];inline void add_edge(int s,int t,int w){    edges.push_back(Edge(s,t,w));    edges.push_back(Edge(t,s,w));    int m=edges.size();    G[s].push_back(m-2);    G[t].push_back(m-1);}int n,dp[maxn];struct  DPD{    int a[2];    int p[2];    void clear()    {        a[0]=a[1]=0;        p[0]=p[1]=-1;    }    void insert(int num,int x)    {        if(num>=a[0])        {            a[1]=a[0];            p[1]=p[0];            a[0]=num;            p[0]=x;        }        else if(a[0]>num&&num>=a[1])        {            a[1]=num;            p[1]=x;        }    }}  dpd[maxn] ;int dfs(int x,int fa){    dpd[x].clear();    for(int i=0;i<G[x].size();i++)    {        Edge & e=edges[G[x][i]];        int & y=e.to;        int & w=e.w;        if(y==fa)  continue;        dpd[x].insert(dfs(y,x)+w,y  );    }    return dpd[x].a[0];}void DP(int x,int fa,int dis){     dp[x]=max(dpd[x].a[0], dis  );   DPD ret=dpd[x];     ret.insert(dis,fa);    for(int i=0;i<G[x].size();i++)    {        Edge & e=edges[G[x][i]];        int & y=e.to;        int & w=e.w;        if(y==fa)  continue;        if( y!=ret.p[0] )  DP(y,x,ret.a[0]+w  );        else     DP(y,x,ret.a[1]+w);    }}int main(){  while(~scanf("%d",&n))  {      for(int i=1;i<=n;i++)  G[i].clear();      edges.clear();      for(int i=2;i<=n;i++)      {          int x,w;          scanf("%d%d",&x,&w);          add_edge(i,x,w);      }      dfs(1,-1);      DP(1,-1,0);      for(int i=1;i<=n;i++)      {          printf("%d\n",dp[i]);      }  }    return 0;}


0 0
原创粉丝点击