边分治+虚树+直径合并:P4220通道

本文搬运自本人高中时期CSDN博客,若图片加载不出来,可到原文查看:https://blog.csdn.net/zhangtingxiqwq/article/details/135266426

https://www.luogu.com.cn/problem/P4220

第一棵树可以直接边分治,然后黑白染色,弄到第二棵树上建虚树

第三棵树很难搞。需要回到题目本身,发现题目没叫我们求奇奇怪怪的东西,而是求距离,肯定有啥性质,而且还是最大距离,就是一个点集的直径。

直径显然支持合并性,我们直接在虚树上树形dp进行合并即可

1
2
3
4
5
6
pre coding at 8:14
st coding at 9:00
fn coding at 10:35
st bugging at 11:30
passing at 12:32
fn blogging at 12:45
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//7.2k
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
#define int long long
inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'||
ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//srand(time(0));
#define N 200010
//#define M
//#define mo
struct Point { int x, w, o; };
int n, m, i, j, k, T, c;
int Log2[N<<1], ans;

struct Tree {
int i, j, k;
int tot, m, dfn[N], oula[N<<1], oula_xu[N], dep[N], h[N];
struct node { int y, z; };
vector<node>G[N<<1];
void add_edge(int u, int v, int w) {
debug("%lld %lld %lld\n", u, v, w);
G[u].pb({v, w}); G[v].pb({u, w});
}
void dfs1(int x, int fa) {
dfn[x]=++tot; h[x] = h[fa]+1;
oula[++m]=x; oula_xu[x]=m;
for(auto t : G[x]) if(t.y != fa) {
int y = t.y, z = t.z; dep[y] = dep[x] + z;
dfs1(y, x); oula[++m]=x;
}
}
// struct ST {
struct Node { int x, dep; };
Node min(Node A, Node B) { return (A.dep < B.dep) ? A : B; }
int min(int a, int b) { return a < b ? a : b; }
Node f[N][22];
int l, r;
void Pre_Lca() {
for(i=2; i<=m; ++i) Log2[i]=Log2[i>>1]+1;
for(i=1; i<=m; ++i) f[i][0]={oula[i], h[oula[i]]};
for(k=1; k<=20; ++k)
for(l=1, r=(1<<k); r<=m; ++l, ++r)
f[l][k]=min(f[l][k-1], f[l+(1<<(k-1))][k-1]);
}
int qry(int a, int b) {
int k = Log2[b-a+1];
return min(f[a][k], f[b-(1<<k)+1][k]).x;
}
// }Lca;
int lca(int x, int y) {
int a = oula_xu[x], b = oula_xu[y];
// debug("qry[%d %d] => %d %d\n", x, y, min(a, b), max(a, b));
return qry(min(a, b), max(a, b));
}
int dis(int x, int y) {
int z = lca(x, y);
return dep[x] + dep[y] - 2 * dep[z];
}
}T1, T2, T3;

namespace Tree1 {
int u, v, w, tot, i, b[N];
vector<pair<int, int> >G[N];
void dfs(int x, int fa) {
for(auto t : G[x]) if(t.fi != fa) {
int y = t.fi, z = t.se;
if(!b[x]) b[x]=++tot, T1.add_edge(x, tot, 0);
else T1.add_edge(b[x], tot+1, 0), b[x]=++tot;
T1.add_edge(b[x], y, z);
dfs(y, x);
}
}
void Main() {
tot=n;
for(i=1; i<n; ++i) {
u=read(); v=read(); w=read();
G[u].pb({v, w}); G[v].pb({u, w});
}
dfs(1, 0);
T1.dfs1(1, 0);
for(i=1; i<=tot; ++i) debug("%lld ", T1.h[i]); debug("\n");
debug("---\n");
// T1.Lca.Pre_Lca();
}
}

namespace Tree2 {
int i, u, v, w;
void Main() {
for(i=1; i<n; ++i) {
u=read(); v=read(); w=read();
T2.add_edge(u, v, w);
}
T2.dfs1(1, 0); T2.Pre_Lca();
for(i=1; i<=T2.m; ++i) debug("%lld ", T2.oula[i]); debug("\n");
for(i=1; i<=n; ++i) debug("%lld ", T2.oula_xu[i]); debug("\n");
}
}

namespace Tree3 {
int u, v, w;
void Main() {
for(i=1; i<n; ++i) {
u=read(); v=read(); w=read();
T3.add_edge(u, v, w);
}
T3.dfs1(1, 0); T3.Pre_Lca();
}
}

namespace XuTree_part {
int sta[N], top, o[N], w[N], l;
vector<pair<int, int> >E;
pair<int, int> dp[N][2];
vector<int>G[N];
void build(vector<Point> h) {
sort(h.begin(), h.end(), [] (Point x, Point y) { return T2.dfn[x.x] < T2.dfn[y.x]; });
sta[top = 1] = 1; E.clear();
for(auto t : h)
if(t.x != 1) {
l = T2.lca(t.x, sta[top]);
// debug("LCA (%lld %lld) = %lld\n", t.x, sta[top], l);
if(l != sta[top]) {
while(T2.dfn[l] < T2.dfn[sta[top-1]])
E.pb({sta[top-1], sta[top]}), --top;
if(l != sta[top-1]) E.pb({l, sta[top]}), sta[top]=l;
else E.pb({l, sta[top]}), --top;
}
sta[++top]=t.x;
}
while(top>1) E.pb({sta[top-1], sta[top]}), --top;
for(auto t : E) G[t.fi].clear(), G[t.se].clear(), o[t.fi]=o[t.se]=0;
for(auto t : E) G[t.fi].pb(t.se), debug("%lld -> %lld\n", t.fi, t.se);
for(auto t : h) o[t.x]=t.o, w[t.x]=t.w+T2.dep[t.x];
for(auto t : h) debug("%lld : %lld\n", t.x, w[t.x]); debug("\n");
}
int Len(int x, int y) { return T3.dis(x, y) + w[x] + w[y]; }
int calc(int x, int y, int z) {
if(!x || !y) return 0;
debug("(%lld %lld) %lld\n", x, y, z);
return Len(x, y) - 2*T2.dep[z] + c;
}
pair<int, int> merge(pair<int, int>A, pair<int, int>B) {
int s=-1e18, x=0, y=0;
if(A.fi && A.se && Len(A.fi, A.se) > s) s=Len(A.fi, A.se), x=A.fi, y=A.se;
if(B.fi && B.se && Len(B.fi, B.se) > s) s=Len(B.fi, B.se), x=B.fi, y=B.se;
if(A.fi && B.fi && Len(A.fi, B.fi) > s) s=Len(A.fi, B.fi), x=A.fi, y=B.fi;
if(A.fi && B.se && Len(A.fi, B.se) > s) s=Len(A.fi, B.se), x=A.fi, y=B.se;
if(A.se && B.fi && Len(A.se, B.fi) > s) s=Len(A.se, B.fi), x=A.se, y=B.fi;
if(A.se && B.se && Len(A.se, B.se) > s) s=Len(A.se, B.se), x=A.se, y=B.se;
if(s < 0 && A.fi) x = A.fi; if(s < 0 && B.fi) x = B.fi;
return {x, y};
}
void dfs(int x) {
dp[x][0] = dp[x][1] = {0, 0};
if(o[x] == 1) dp[x][0] = {x, 0};
if(o[x] == 2) dp[x][1] = {x, 0};
for(int y : G[x]) {
dfs(y);
ans = max(ans, calc(dp[x][0].fi, dp[y][1].fi, x));
ans = max(ans, calc(dp[x][0].fi, dp[y][1].se, x));
ans = max(ans, calc(dp[x][0].se, dp[y][1].fi, x));
ans = max(ans, calc(dp[x][0].se, dp[y][1].se, x));
ans = max(ans, calc(dp[x][1].fi, dp[y][0].fi, x));
ans = max(ans, calc(dp[x][1].fi, dp[y][0].se, x));
ans = max(ans, calc(dp[x][1].se, dp[y][0].fi, x));
ans = max(ans, calc(dp[x][1].se, dp[y][0].se, x));
dp[x][0] = merge(dp[x][0], dp[y][0]);
dp[x][1] = merge(dp[x][1], dp[y][1]);
}
}
void Main(vector<Point> h) {
if(h.size() <= 1) return ;
build(h);
dfs(1);
}
}

namespace Edge_part {
int w[N], rt, sum, i, j, dis[N], b[N];
int mx[N], ST, ED;
map<pair<int, int>, int>mp;
void map_edge() {
for(i=1; i<=Tree1::tot; ++i)
for(auto t : T1.G[i]) mp[{i, t.y}]=t.z, debug("%d =? %d %d\n", i, t.y, t.z);
}
void dfs(int x, int fa) {
// debug("\t %d -> %d\n", fa, x);
w[x]=1;
for(auto t : T1.G[x]) if(t.y!=fa && !b[t.y]) {
int y = t.y, z = t.z;
dfs(y, x); w[x]+=w[y];
}
mx[x]=max(w[x], sum-w[x]);
if(mx[x] < mx[rt]) rt=x, ST=x, ED=fa;
}
vector<Point> h;
void dfs2(int x, int fa, int o) {
if(x<=n) h.pb({x, dis[x], o});
if(x<=n) debug("[x %lld] %lld %lld\n", x, dis[x], o);
for(auto t : T1.G[x]) if(t.y!=fa && !b[t.y]) {
int y = t.y, z = t.z;
dis[y] = dis[x] + z;
dfs2(y, x, o);
}
}
void solve(int x) {
dfs(x, 0); sum=w[x]; rt=0; dfs(x, 0);
if(sum == 1) return ;
int st = ST, ed = ED;
b[st]=1; solve(ed); b[st]=0;
b[ed]=1; solve(st); b[ed]=0;
// debug("\t[%d] <-> [%d] %d\n", st, ed, c);
h.clear();
c = mp[{st, ed}];
debug("{%lld} [%lld] <-> [%lld] %lld\n", x, st, ed, c);
dis[st]=0; dfs2(st, ed, 1);
dis[ed]=0; dfs2(ed, st, 2);
XuTree_part::Main(h);
}
void Main() {
mx[0]=1e18;
map_edge();
solve(1);
}
}

signed main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// T=read();
// while(T--) {
//
// }
n=read();
Tree1::Main(); Tree2::Main(); Tree3::Main();
Edge_part::Main();
printf("%lld", ans);
return 0;
}