hdu 2196 Computer 树形DP

本文介绍了一种使用树形动态规划的方法来解决一个特定问题:对于一棵树,找到每个节点到叶节点的最长距离。通过两次深度优先搜索(DFS),算法能够高效地计算出这些距离。

题目链接: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\n",fmaxn[i]);
95     }
96     return 0;
97 }

 

转载于:https://www.cnblogs.com/huangxf/p/4370159.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值