Contents
Solution
Let
M
[
x
]
M[x]
M[x] be the Maximal-Distance of
x
x
x; Definition
.
Let
a
[
x
]
a[x]
a[x] be the point-value; then the Minimal-Distance of
x
x
x must be
a
[
x
]
a[x]
a[x] in that all point-values are
≥
1
\geq 1
≥1;
.
Obviously, the value asked for a point
x
x
x equals
V
[
x
]
=
M
[
x
]
−
a
[
x
]
V[x] = M[x] - a[x]
V[x]=M[x]−a[x]; then the final answer equals
m
a
x
(
V
[
x
]
)
∀
x
max( V[x]) \ \ \forall x
max(V[x]) ∀x;
There are two totally different means.
朴素做法: 树的最长路径
The calculation for M [ x ] M[x] M[x] is a template-algorithm;
DP做法
V
[
x
]
V[x]
V[x] denotes a Path which is the Maximal-Distance-Path of
x
x
x with removing the endpoint
x
x
x;
.
+
If
V
[
x
]
V[x]
V[x] is the answer, let’s analyze the Maximal-Distance-Path
M
[
x
]
M[x]
M[x] (i.e., the Answer-path
V
[
x
]
V[x]
V[x] joining the single point x
);
.
.
The endpoints of
M
[
x
]
M[x]
M[x] are
x
,
y
x,y
x,y where
y
y
y must be a Leaf-Node in that all point-values
≥
1
\geq 1
≥1;
.
.
x
x
x is also a Leaf-Node (if not, you can prove that there must exist another point whose
V
[
]
>
V
[
x
]
V[] > V[x]
V[]>V[x], which contradicts that
V
[
x
]
V[x]
V[x] is the maximum);
.
+
But notice, even if
V
[
x
]
V[x]
V[x] is the Answer, the
M
[
x
]
M[x]
M[x] maybe not the Diameter of the tree; e.g.,
a
−
b
−
c
,
b
−
d
−
e
;
A
[
a
,
b
,
c
]
=
10
,
A
[
d
,
e
]
=
1
a-b-c, b-d-e; A[a,b,c]=10, A[d,e]=1
a−b−c,b−d−e;A[a,b,c]=10,A[d,e]=1, although the diameter is
a
−
b
−
c
a-b-c
a−b−c whose value is
30
30
30 (it corresponds to
V
[
a
]
=
V
[
c
]
=
20
V[a] = V[c] = 20
V[a]=V[c]=20); while the Answer-Path is
d
−
b
−
a
d-b-a
d−b−a whose value is
V
[
e
]
=
21
V[e]=21
V[e]=21, but
M
[
e
]
=
e
−
d
−
b
−
a
M[e] = e-d-b-a
M[e]=e−d−b−a is not the diameter;
Let
0
0
0 be the root of the Tree;
.
D
1
[
x
]
D1[x]
D1[x] be the Maximal-Distance of the path
(
x
−
.
.
.
−
y
)
(x-...-y)
(x−...−y) where
x
x
x is the Peak of the path (cuz all point-values are
≥
1
\geq 1
≥1,
y
y
y must be a Leaf-Node);
.
D
2
[
x
]
D2[x]
D2[x] be the Maximal-Distance of the path
(
x
−
.
.
.
−
y
)
(x-...-y)
(x−...−y) where
x
x
x is the Peak and
y
y
y is not a Leaf-Node (it can be proved that the Child-Node of
y
y
y must be Leaf-Node);
Suppose
V
[
x
]
V[x]
V[x] is the Answer, it corresponds to the Path
y
−
.
.
.
−
z
y-...-z
y−...−z (the
M
[
x
]
=
(
x
−
y
−
.
.
.
−
z
)
M[x] = (x-y-...-z)
M[x]=(x−y−...−z)); Let’s analyze the Path
M
[
x
]
M[x]
M[x] (not
V
[
x
]
V[x]
V[x]), cuz
M
[
x
]
M[x]
M[x] is the familiar-notion and just remove the point
x
x
x from
M
[
x
]
M[x]
M[x] then it will become the Answer
V
[
x
]
V[x]
V[x];
.
The key is to find the Peak of this path, that is
V
[
x
]
V[x]
V[x] is not calculated in the point
x
x
x, but in the point
c
c
c while
c
c
c is the Peak of the path
V
[
x
]
V[x]
V[x];
.
Case-A
If x
be the Peak (最高点) of the Path
M
[
x
]
M[x]
M[x], then
V
[
x
]
=
m
a
x
(
D
1
[
s
]
)
s
∈
son of x
V[x] = max( D1[s]) \ \ s \in \text{son of x}
V[x]=max(D1[s]) s∈son of x;
.
Case-B
If z
be the Peak of the Path
M
[
x
]
M[x]
M[x], then
V
[
x
]
=
m
a
x
(
D
2
[
s
]
)
+
A
[
x
]
s
∈
son of x
V[x] = max( D2[s]) + A[x] \ \ s \in \text{son of x}
V[x]=max(D2[s])+A[x] s∈son of x;
.
Case-C
Otherwise, there must has a point
c
c
c which is the Peak, (
x
−
.
.
.
−
a
−
c
−
b
−
.
.
.
−
y
x-...-a-c-b-...-y
x−...−a−c−b−...−y whose value
V
[
x
]
V[x]
V[x] has two-cases:
.
.
Case-1
: Let
b
b
b be a son of
c
c
c that has the maximal
D
1
[
b
]
D1[b]
D1[b], then update
V
[
x
]
=
D
1
[
b
]
+
A
[
c
]
+
D
2
[
a
]
V[x] = D1[b] + A[c] + D2[a]
V[x]=D1[b]+A[c]+D2[a] where
a
≠
b
a \neq b
a=b and has the maximal
D
2
[
a
]
D2[a]
D2[a];
.
.
Case-2
: Let
a
a
a be a son of
c
c
c that has the maximal
D
2
[
a
]
D2[a]
D2[a], then update
V
[
x
]
=
D
2
[
a
]
+
A
[
c
]
+
D
1
[
b
]
V[x] = D2[a] + A[c] + D1[b]
V[x]=D2[a]+A[c]+D1[b] where
b
≠
a
b \neq a
b=a and has the maximal
D
1
[
b
]
D1[b]
D1[b];
.
It can be proved that, the Answer of
M
[
x
]
=
x
−
.
.
.
−
a
−
c
−
b
−
.
.
.
−
y
M[x] = x-...-a-c-b-...-y
M[x]=x−...−a−c−b−...−y (i.e.,
V
[
x
]
=
.
.
.
−
a
−
c
−
b
−
.
.
.
−
y
V[x] = ...-a-c-b-...-y
V[x]=...−a−c−b−...−y) must
≥
\geq
≥ Case-1&2 (otherwise,
V
[
x
]
V[x]
V[x] is not the Answer);
.
The Answer
V
[
x
]
V[x]
V[x] maybe Case-1, also maybe Case-2;
.
.
e.g.,
a
−
b
−
c
−
d
−
e
,
V
[
a
,
b
,
c
,
d
,
e
]
=
1
,
1
,
1
,
2
,
2
a-b-c-d-e, V[a,b,c,d,e]=1,1,1,2,2
a−b−c−d−e,V[a,b,c,d,e]=1,1,1,2,2; the Answer is
V
[
a
]
=
b
−
c
−
d
−
e
V[a] = b-c-d-e
V[a]=b−c−d−e and suppose
c
c
c is the Peak; when we at
c
c
c: the Case-1 equals
4
+
1
+
1
4 + 1 + 1
4+1+1, while Case-2 equals
2
+
1
+
2
2 + 1 + 2
2+1+2; The answer
V
[
a
]
V[a]
V[a] corresponds to Case-1;
.
.
e.g.,
a
−
b
−
c
−
d
−
e
,
V
[
a
,
b
,
c
,
d
,
e
]
=
1
,
10
,
1
,
1
,
9
a-b-c-d-e, V[a,b,c,d,e]=1,10,1,1,9
a−b−c−d−e,V[a,b,c,d,e]=1,10,1,1,9; the Answer is
V
[
a
]
=
b
−
c
−
d
−
e
V[a] = b-c-d-e
V[a]=b−c−d−e and suppose
c
c
c is the Peak; when we at
c
c
c: the Case-1 equals
11
+
1
+
1
11 + 1 +1
11+1+1, while Case-2 equals
10
+
1
+
10
10 + 1 + 10
10+1+10; The answer
V
[
a
]
V[a]
V[a] corresponds to Case-2;
While the idea is somewhat complicated, the algorithm is much more clean:
pair< long long, long long> Dfs( int _cur, int _fa){
long long len1 = 0, len2 = 0;
int counter = 0;
for( int nex, e = G->Head[ _cur]; ~e; e = G->Next[ e]){
nex = G->Vertex[ e];
if( nex == _fa){ continue;}
auto ret = Dfs( nex, _cur);
auto l1 = ret.first, l2 = ret.second;
//--
++ counter;
if( counter <= 1){
len1 = max( l1, len1);
len2 = max( l2, len2);
continue;
}
// the Case-C; notice `len1, len2` may share the same Child-Node, so you should use `l1,len2` not `len1,len2`;
//> [l1, _cur, len2]
Ans = max( l1 + A[ _cur] + len2, Ans);
//> [l2, _cur, len1]
Ans = max( l2 + A[ _cur] + len1, Ans);
//--
len1 = max( l1, len1);
len2 = max( l2, len2);
}
if( counter == 0){ //< Leaf-Node
return {A[ _cur], 0};
}
else if( counter == 1){ //< The case-(A,B)
Ans = max( len1, Ans);
Ans = max( len2 + A[ _cur], Ans);
}
len1 += A[ _cur];
len2 += A[ _cur];
return {len1, len2};
}
Ans = 0;
Dfs( 0, -1);