隐藏
「清北学堂1」天天和树 - 树形动规 | Bill Yang's Blog

路终会有尽头,但视野总能看到更远的地方。

0%

「清北学堂1」天天和树 - 树形动规

题目大意

一个树由n个点,n-1条边组成,结点编号为1..n。树上任意两个点之间路径唯一。
定义一个点到一条路径的距离为:该点到路径上最近的一个点需要经过的边的数量。
现在想知道怎样选两个点确定一条路径,使得距离这个路径最远的点尽量近。要求你输出距离路径最远的点距离路径的距离。


题目分析

找出树的偏心距。
用两次Dfs找出树的直径,再用一次Bfs得出偏心距。
然而本人比较懒,直接复制的树网的核加强版的代码,比较智障。


代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<queue>
#include<stack>
using namespace std;
typedef long long LL;
inline const LL Get_Int() {
LL num=0,bj=1;
char x=getchar();
while(x<'0'||x>'9') {
if(x=='-')bj=-1;
x=getchar();
}
while(x>='0'&&x<='9') {
num=num*10+x-'0';
x=getchar();
}
return num*bj;
}
char x1;
const int maxn=500005;
LL d[maxn],dist3[maxn],Dists[maxn],Diste[maxn],dist1[maxn],dist2[maxn],Diameter=0,ans=1e13,tmp=0;
int From[maxn],vst[maxn],father[maxn],Down1[maxn],Down2[maxn],Start,End;
struct Edge {
int to,next;
LL dist;
} edges[maxn*2];
int cnt=0,Head[maxn];
void AddEdge(int x,int y,LL v) {
cnt++;
edges[cnt].to=y;
edges[cnt].next=Head[x];
edges[cnt].dist=v;
Head[x]=cnt;
}
struct St {
int Now,i,father;
LL dist;
} S[maxn];
int top;
char x2;
void TreeDp(int Now,int fa) { //记得加手工栈
int i,Next;
START1:
father[Now]=fa;
Down1[Now]=Down2[Now]=Now;
for(i=Head[Now]; i; i=edges[i].next) {
Next=edges[i].to;
if(Next==fa)continue;
S[++top]=(St) {
Now,i,fa,0
};
fa=Now;
Now=Next;
goto START1;
RET:
Now=S[top].Now;
i=S[top].i;
fa=S[top].father;
Next=edges[i].to;
top--;
if(dist1[Next]+edges[i].dist>dist1[Now]) {
dist2[Now]=dist1[Now];
Down2[Now]=Down1[Now];
dist1[Now]=dist1[Next]+edges[i].dist;
Down1[Now]=Down1[Next];
From[Now]=Next;
} else if(dist1[Next]+edges[i].dist>dist2[Now]) {
dist2[Now]=dist1[Next]+edges[i].dist;
Down2[Now]=Down1[Next];
}
}
if(top)goto RET;
}
void Dfse(int Now,int fa,LL dist) { //记得加手工栈
int i,Next;
START2:
Diste[Now]=dist;
for(i=Head[Now]; i; i=edges[i].next) {
Next=edges[i].to;
if(Next==fa)continue;
S[++top]=(St) {
Now,i,fa,dist
};
dist+=edges[i].dist;
fa=Now;
Now=Next;
goto START2;
RET2:
Now=S[top].Now;
i=S[top].i;
fa=S[top].father;
dist=S[top].dist;
Next=edges[i].to;
top--;
}
if(top)goto RET2;
}
void Dfss(int Now,int fa,LL dist) { //记得加手工栈
int i,Next;
START3:
Dists[Now]=dist;
father[Now]=fa;
for(i=Head[Now]; i; i=edges[i].next) {
Next=edges[i].to;
if(Next==fa)continue;
S[++top]=(St) {
Now,i,fa,dist
};
dist+=edges[i].dist;
fa=Now;
Now=Next;
goto START3;
RET3:
Now=S[top].Now;
i=S[top].i;
fa=S[top].father;
dist=S[top].dist;
Next=edges[i].to;
top--;
}
if(top)goto RET3;
}
LL n,s;
int main() {
n=Get_Int();
s=0x7fffffff/2;
for(int i=1; i<n; i++) {
int x=Get_Int(),y=Get_Int();
AddEdge(x,y,1);
AddEdge(y,x,1);
}
TreeDp(1,0);
for(int i=1; i<=n; i++)
if(dist1[i]+dist2[i]>Diameter) {
Diameter=dist1[i]+dist2[i];
Start=Down2[i];
End=Down1[i];
}
Dfse(End,0,0);
Dfss(Start,0,0);
queue<int>Q;
memset(d,0x3f,sizeof(d));
for(int i=1; i<=n; i++)
if(Dists[i]+Diste[i]==Diameter) {
Q.push(i);
vst[i]=1;
d[i]=0;
}
while(!Q.empty()) {
int Now=Q.front();
Q.pop();
for(int i=Head[Now]; i; i=edges[i].next) {
int Next=edges[i].to;
if(vst[Next])continue;
d[Next]=d[Now]+edges[i].dist;
vst[Next]=1;
Q.push(Next);
}
}
for(int i=1; i<=n; i++)tmp=max(tmp,d[i]);
int i=End,j=End;
while(i!=Start||j!=Start) {
ans=min(max(min(Dists[i],Dists[j]),min(Diste[i],Diste[j])),ans);
if(j!=Start)j=father[j];
else i=father[i];
while(Diste[j]-Diste[i]>s)i=father[i];
}
ans=min(max(min(Dists[i],Dists[j]),min(Diste[i],Diste[j])),ans);
printf("%lld\n",max(ans,tmp));
return 0;
}
姥爷们赏瓶冰阔落吧~