zoukankan      html  css  js  c++  java
  • hdu 2196 Computer 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2196

    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.

    题意描述:给出一颗树,求出每个节点到叶节点的最长距离。

    算法分析:树形DP。每个节点向下dfs一遍,向上dfs一遍即可。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<cmath>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<vector>
     9 #define inf 0x7fffffff
    10 using namespace std;
    11 const int maxn=10000+10;
    12 
    13 int n;
    14 int fmaxn[maxn],fmaxnid[maxn],smaxn[maxn],smaxnid[maxn];
    15 vector<pair<int,int> >vec[maxn];
    16 
    17 void dfs(int u,int pre)
    18 {
    19     int k=vec[u].size();
    20     for (int i=0 ;i<k ;i++)
    21     {
    22         int v=vec[u][i].first;
    23         int w=vec[u][i].second;
    24         if (v==pre) continue;
    25         dfs(v,u);
    26         if (smaxn[u]<fmaxn[v]+w)
    27         {
    28             smaxn[u]=fmaxn[v]+w;
    29             smaxnid[u]=v;
    30             if (smaxn[u]>fmaxn[u])
    31             {
    32                 swap(fmaxn[u],smaxn[u]);
    33                 swap(fmaxnid[u],smaxnid[u]);
    34             }
    35         }
    36     }
    37     return;
    38 }
    39 
    40 void dfs2(int u,int pre)
    41 {
    42     int k=vec[u].size();
    43     for (int i=0 ;i<k ;i++)
    44     {
    45         int v=vec[u][i].first;
    46         int w=vec[u][i].second;
    47         if (v==pre) continue;
    48         if (v==fmaxnid[u])
    49         {
    50             if (smaxn[u]+w>smaxn[v])
    51             {
    52                 smaxn[v]=smaxn[u]+w;
    53                 smaxnid[v]=u;
    54                 if (smaxn[v]>fmaxn[v])
    55                 {
    56                     swap(smaxn[v],fmaxn[v]);
    57                     swap(smaxnid[v],fmaxnid[v]);
    58                 }
    59             }
    60         }
    61         else if (fmaxn[u]+w>smaxn[v])
    62         {
    63             smaxn[v]=fmaxn[u]+w;
    64             smaxnid[v]=u;
    65             if (smaxn[v]>fmaxn[v])
    66             {
    67                 swap(smaxn[v],fmaxn[v]);
    68                 swap(smaxnid[v],fmaxnid[v]);
    69             }
    70         }
    71         dfs2(v,u);
    72     }
    73     return ;
    74 }
    75 
    76 int main()
    77 {
    78     while (scanf("%d",&n)!=EOF)
    79     {
    80         for (int i=1 ;i<=n ;i++) vec[i].clear();
    81         memset(fmaxn,0,sizeof(fmaxn));
    82         memset(fmaxnid,0,sizeof(fmaxnid));
    83         memset(smaxn,0,sizeof(smaxn));
    84         memset(smaxnid,0,sizeof(smaxnid));
    85         int a,b;
    86         for (int i=2 ;i<=n ;i++)
    87         {
    88             scanf("%d%d",&a,&b);
    89             vec[i].push_back(make_pair(a,b));
    90             vec[a].push_back(make_pair(i,b));
    91         }
    92         dfs(1,1);
    93         dfs2(1,1);
    94         for (int i=1 ;i<=n ;i++) printf("%d
    ",fmaxn[i]);
    95     }
    96     return 0;
    97 }
  • 相关阅读:
    类数组(伪数组)
    go面试题[2]
    go面试题[1]
    go编程第十五课时
    php实现堆排序
    go编程第十三课时
    go第十一课时
    关于循环队列 -> 击鼓传花
    网栅格布局
    《学习JAVASCRIPT数据结构与算法》 ES6 部分笔记
  • 原文地址:https://www.cnblogs.com/huangxf/p/4370159.html
Copyright © 2011-2022 走看看