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.
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.
5 1 1 2 1 3 1 1 1
3 2 3 4 4
在一棵树上,有N个节点,边带有权,输出以这个节点作点起点的最长距离。
看上去好像是无向图的最长距离。但是如果用N(N在1到10000之间)次最路径,就算用的是dij+heap优化,时间复杂度为 N^2*logN ,超时了。于是用树形DP。
这里用了两次的dfs。
第一次是自上而下地求出每个节点的的最长距离f[i ],同时,求出在第二次bfs 要用的次长度g[ i ]。
自己画图可以知道,一个点的最长距离只可能是由它的子节点得到,或者从它的父结点的最长距离或者次长距离加上它与父节点的距离得到。从这里我们就可以得到状态转移方程。而且节点的最长距离就是f[ i ],h[ i ] 中的最大值。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#define N 10005
using namespace std;
int n;
vector<int> son[N],w[N];
int f[N],g[N],longest[N],h[N];
int dfs (int root)
{
int flag;
if (f[root]) return f[root];
int len = son[root].size();
if (len == 0) return 0;
for (int i = 0;i != len;i ++)
{
int j = son[root][i];
if (dfs(j) + w[root][i] > f[root])
{
g[root] = f[root];
f[root] = f[j] + w[root][i];
flag = j;//记录该结点是由哪个子节点求出最长距离
}
else if (f[j] + w[root][i] > g[root]) g[root] = f[j] + w[root][i];
}
longest[root] = flag;
return f[root];
}
void dfs1 (int root)
{
int len = son[root].size();
for (int i = 0;i != len;i ++)
{
int j = son[root][i];
if (j == longest[root]) h[j] = max(h[root],g[root]) + w[root][i];
else h[j] = max(h[root],f[root]) + w[root][i];
dfs1(j);
}
}
int main ()
{
while (~scanf("%d",&n))
{
int x,y;
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
memset(h,0,sizeof(h));
for (int i = 1;i <= n;i ++)
{
son[i].clear();
w[i].clear();
}
for (int i = 2;i <= n;i ++)
{
scanf("%d%d",&x,&y);
son[x].push_back(i);
w[x].push_back(y);
}
dfs(1);
dfs1(1);
for(int i=1;i<=n;i++) printf("%d\n",max(f[i],h[i]));
}
return 0;
}
另外:
#include <stdio.h>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
typedef struct
{
int len;//边长
int son;//子节点
}Edge;
vector <Edge> edge[10010];//edge【i】【j】节点i的第j条边
int mmax[10010];//最大值
int smax[10010]; //第二大
int mmaxdex[10010];//最大值pre点
int smaxdex[10010];//第二大pre点
void dfs1(int now, int fa)//向子节点找
{
for (int i = 0; i < edge[now].size(); i++)
{
int k = edge[now][i].son;
/*if (k == pre)
continue;*/
dfs1(k, now);
if (mmax[now] < mmax[k] + edge[now][i].len)//最大值 < 子最大+当前边
{
smax[now] = mmax[now];//更新第二大
mmax[now] = mmax[k] + edge[now][i].len;//更新最大值
smaxdex[now] = mmaxdex[now];//记录第二大点
mmaxdex[now] = k;//记录最大点
}
else if (smax[now] < mmax[k] + edge[now][i].len)//第二大 < 子最大+当前边
{
smax[now] = mmax[k] + edge[now][i].len;//只更新第二大
smaxdex[now] = k;
}
}
}
void dfs2(int now, int fa, int len)//向父节点找
{
int k;
//if (now != 1)
{
if (mmaxdex[fa] == now)//fa的pre点 = now 即经过该点
{
if (mmax[now] < smax[fa] + len)//now最大值 < fa第二+当前边
{
smax[now] = mmax[now];
smaxdex[now] = mmaxdex[now];
mmax[now] = smax[fa] + len;
mmaxdex[now] = fa;
}
else if (smax[now] < smax[fa] + len)//now第二大 < fa第二+当前边
{
smax[now] = smax[fa] + len;
smaxdex[now] = fa;
}
}
else //fa的pre点 != now 即不经过该点
{
if (mmax[now] < mmax[fa] + len)//now最大值 < fa最大+当前边
{
smax[now] = mmax[now];
smaxdex[now] = mmaxdex[now];
mmax[now] = mmax[fa] + len;
mmaxdex[now] = fa;
}
else if (smax[now] < mmax[fa] + len)//now最大值 < fa最大+len
{
smax[now] = mmax[fa] + len;
mmaxdex[now] = fa;
}
}
}
for (int i = 0; i < edge[now].size(); i++)
{
int k = edge[now][i].son;
if (k == fa)
continue;
dfs2(k, now, edge[now][i].len);
}
}
int main()
{
int N, x, y;
Edge temp;
while (~scanf("%d", &N))
{
for (int i = 0; i <= N; i++)
{
edge[i].clear();
}
memset(mmax, 0, sizeof(mmax));
memset(smax, 0, sizeof(smax));
memset(mmaxdex, 0, sizeof(mmaxdex));
memset(smaxdex, 0, sizeof(smaxdex));
for (int i = 2; i <= N; i++)
{
scanf("%d%d", &x, &y);
temp.len = y;//建树,双向存
temp.son = i;
edge[x].push_back(temp);
}
dfs1(1, -1);
dfs2(1, -1, 0);
for (int i = 1; i <= N; i++)
{
printf("%d\n", max(mmax[i], smax[i]));
}
}
return 0;
}