Luogu 4565 - [CTSC2018]暴力写挂

给定两棵树 $f, g$ ,要求:

$f_i$ 是 $i$ 在 $f$ 中的深度。

这是有根树的情况,我们先把它转换成无根树:

发现这个式子,后面的 $g_{\operatorname{LCA(u, v)}}$ 非常难以解决,不如直接在 $g$ 中枚举一个点作为 $\operatorname{LCA}(u, v)$ ,并统计其不同子树中的点的答案,此时我们规定了答案点集:答案只能由 $f​$ 的这些点产生。

继续观察上式,不难发现其贡献为: $\operatorname{dis}(u, \operatorname{LCA}(u, v)) + f_u​$ 。考虑边分治,我们将 $\operatorname{dis}(u, \operatorname{LCA}(u, v)) + \operatorname{dis}(v, \operatorname{LCA}(u, v))​$ 看作一条 $u \rightarrow v​$ 的路径。那么对于边分治中心 $i​$ ,计算通过 $i​$ 的路径的最大值。

不难发现如果此时没有了答案点集的限定,问题已经解决了。那么考虑答案点集的情况,可以发现边分树形如线段树。这就意味着是可以进行线段树合并的。对于一个分治中心 $w$ 维护 $\operatorname{lmax}(w)$ 和 $\operatorname {rmax}(w)$ ,表示其左子树的 $\operatorname{dis}(u, w) + f_u$ 的最大值和右子树 $\operatorname{dis}(v, w) + f_v$ 的最大值。每次计算 $g$ 时依次添加点,该点贡献在边分树中到根的链,合并时统计答案。

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
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <iostream>

#define INF 0x3f3f3f3f3f3f3f3f
#define MAXN 750010

typedef long long lint;

using namespace std;

int n;
int amount;
lint mini;
int root;
int size[MAXN], fa[MAXN << 1], ch[MAXN << 1][2], level[MAXN];
lint dep[MAXN];
lint dis[25][MAXN], lmax[MAXN << 4], rmax[MAXN << 4];
int lson[MAXN << 4], rson[MAXN << 4];
int val[MAXN << 1], r[MAXN << 1];
lint ans;
bool visit[MAXN << 1];
int record[MAXN << 4];

struct data {
int next, to, cost;
data(int next = 0, int to = 0, int cost = 0):next(next), to(to), cost(cost) {}
}q[MAXN << 1];

struct graph {
int head[MAXN], cnt;
data edge[MAXN << 1];

graph() {
memset(head, 0, sizeof(head));
cnt = 1;
}

void connect(int u, int v, int w) {
edge[++cnt] = data(head[u], v, w);
head[u] = cnt;
}

void construct(int u, int v, int w) {
connect(u, v, w);
connect(v, u, w);
}
}f, g, h;

int read() {
char c = getchar();
int x = 0, s = 1;
while (!isdigit(c)) {
if (c == '-')
s = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x * s;
}

void rebuild(int u, int pre) {
for (int i = f.head[u]; i; i = f.edge[i].next) {
int v = f.edge[i].to, w = f.edge[i].cost;
if (v == pre)
continue;
dep[v] = dep[u] + w;
rebuild(v, u);
}
int head = 1, tail = 0;
for (int i = f.head[u]; i; i = f.edge[i].next) {
int v = f.edge[i].to;
if (v == pre)
continue;
q[++tail] = f.edge[i];
}
while (tail - head >= 2) {
data a = q[head++], b = q[head++];
int c = ++n;
h.construct(a.to, c, a.cost);
h.construct(b.to, c, b.cost);
q[++tail] = data(0, c, 0);
}
while (head <= tail) {
h.construct(q[head].to, u, q[head].cost);
++head;
}
}

void getroot(int u, int pre, int sum) {
size[u] = 1;
for (int i = h.head[u]; i; i = h.edge[i].next) {
int v = h.edge[i].to;
if (v == pre || visit[i >> 1])
continue;
getroot(v, u, sum);
size[u] += size[v];
int res = max(size[v], sum - size[v]);
if (res < mini) {
mini = res;
root = i;
}
}
}

void DFS(int u, int pre, int dep) {
for (int i = h.head[u]; i; i = h.edge[i].next) {
int v = h.edge[i].to, w = h.edge[i].cost;
if (v == pre || visit[i >> 1])
continue;
dis[dep][v] = dis[dep][u] + w;
DFS(v, u, dep);
}
}

int insert(int u) {
int origin = u;
for (int i = level[u], last = 0; i; --i, u = fa[u], last = amount) {
int x = ++amount;
lmax[x] = rmax[x] = -INF;
record[x] = fa[u];
if (ch[fa[u]][0] == u) {
lson[x] = last;
lmax[x] = dis[i][origin] + dep[origin];
}
else {
rson[x] = last;
rmax[x] = dis[i][origin] + dep[origin];
}
}
return amount;
}

int merge(int u, int v, lint d) {
if (!u || !v)
return u + v;
ans = max(ans, ((lmax[u] + rmax[v] + val[record[u]]) >> 1) - d);
ans = max(ans, ((rmax[u] + lmax[v] + val[record[u]]) >> 1) - d);
lmax[u] = max(lmax[u], lmax[v]);
rmax[u] = max(rmax[u], rmax[v]);
lson[u] = merge(lson[u], lson[v], d);
rson[u] = merge(rson[u], rson[v], d);
return u;
}

int build(int u, int l, int sum) {
mini = INF;
DFS(u, 0, l);
getroot(u, 0, sum);
if (mini == INF) {
level[u] = l;
return u;
}
visit[root >> 1] = true;
int cur = ++n;
val[cur] = h.edge[root].cost;
int v = h.edge[root].to, w = h.edge[root ^ 1].to;
int sv = size[v], sw = sum - sv;
fa[ch[cur][0] = build(v, l + 1, sv)] = cur;
fa[ch[cur][1] = build(w, l + 1, sw)] = cur;
return cur;
}

void solve(int u, int pre, lint d) {
r[u] = insert(u);
ans = max(ans, dep[u] - d);
for (int i = g.head[u]; i; i = g.edge[i].next) {
int v = g.edge[i].to;
if (v == pre)
continue;
solve(v, u, d + g.edge[i].cost);
r[u] = merge(r[u], r[v], d);
}
}

int main() {
n = read();
for (int i = 1; i < n; ++i) {
int u = read(), v = read(), w = read();
f.construct(u, v, w);
}
for (int i = 1; i < n; ++i) {
int u = read(), v = read(), w = read();
g.construct(u, v, w);
}
rebuild(1, 0);
build(1, 0, n);
solve(1, 0, 0);
printf("%lld\n", ans);
}