边分治建虚树优化:P4565暴力写挂

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

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

首先题目的式子很难看,可以变成 :

depx+depy+dis(x,y)deplca2(x,y)2\frac {dep_x+dep_y+dis(x,y)-dep_{lca_2(x,y)}}2

然后直接边分治板子即可

然后听说双log卡常,发现还有个log主要在与虚树的排序。

可以考虑当前分治区间已经排好序,我们边分治为两个点集,拿我们直接按顺序分开,也就是反向归并

在这里插入图片描述

1
2
3
4
5
pre coding at 14:34
st coding at 14:42
st bugging at 15:36
passing at 16:27
fn blogging at 16:34
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
//5.8k暴力写挂 
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCALd
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
//#define int long long
#define ll 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 800010
//#define M
//#define mo
struct Point { int x; ll w; int o; };
int n, m, i, j, k, T;
ll ans=-1e18, c, C;
int Log2[N], ST, ED;

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][20];

struct Tree_ {
int i, m;
struct edge { int y; ll z; };
vector<edge>G[N];
int dfn[N], tot, l, r;
int h[N], oula[N], oula_xu[N];
ll dep[N];
void add_edge(int x, int y, ll z) {
debug("%d %d %d\n", x, y, z);
G[x].pb({y, z}); G[y].pb({x, z});
}
void dfs1(int x, int fa) {
h[x] = h[fa] + 1; dfn[x] = ++tot;
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;
}
}
/**************************************************/

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<=19; ++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 lca(int x, int y) {
int a = oula_xu[x], b = oula_xu[y];
if(a > b) swap(a, b);
int k = Log2[b - a + 1];
return min(f[a][k], f[b-(1<<k)+1][k]).x;
}
/**************************************************/
}T1, T2;

namespace Tree2 {
void Read() {
int u, v, i; ll w;
for(i=1; i<n; ++i) {
u=read(); v=read(); w=read();
T2.add_edge(u, v, w);
}
T2.dfs1(1, 0);
for(i=1; i<=T2.m; ++i) debug("%d ", T2.oula[i]); debug("\n");
for(i=1; i<=n; ++i) debug("%d ", T2.oula_xu[i]); debug("\n");
T2.Pre_Lca();
}
int sta[N], top, l, O[N>>1];
ll w[N>>1];
vector<pair<int, int> >E;
vector<int>G[N>>1];
void build(vector<Point>h) {
sta[top=1]=1; E.clear();
debug("Point : "); for(auto t : h) debug("%d ", t.x); debug("\n");
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(sta[top] != l) {
while(T2.dfn[sta[top-1]] > T2.dfn[l])
E.pb({sta[top-1], sta[top]}), --top;
if(sta[top-1] != l) 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("%d -> %d\n", t.fi, t.se);
for(auto t : h) O[t.x]=t.o, w[t.x]=t.w;
}
ll dp[N][2];
void dfs(int x) {
dp[x][0] = dp[x][1] = -1e12;
if(O[x] == 1) dp[x][0] = w[x];
if(O[x] == 2) dp[x][1] = w[x];
for(int y : G[x]) {
dfs(y);
ans = max(ans, dp[x][0] + dp[y][1] - 2 * T2.dep[x] + c);
ans = max(ans, dp[x][1] + dp[y][0] - 2 * T2.dep[x] + c);
dp[x][0] = max(dp[x][0], dp[y][0]);
dp[x][1] = max(dp[x][1], dp[y][1]);
}
}
void solve(vector<Point>h) {
build(h); dfs(1);
}
}

namespace Tree1 {
vector<pair<int, ll> >G[N>>1];
int tot, b[N], O[N];
ll mx[N], w[N], sum, dis[N];
void dfs(int x, int fa) {
for(auto t : G[x]) if(t.fi!=fa) {
int y = t.fi, z = t.se;
// debug("%d %d %d\n", x, y, z);
if(!b[x]) b[x]=++tot, T1.add_edge(x, b[x], 0);
else T1.add_edge(b[x], tot+1, 0), b[x]=++tot;
T1.add_edge(b[x], y, z);
dfs(y, x);
}
}
int rt;
void Build() {
tot=n;
int u, v; ll w;
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);
mx[0]=1e18;
debug("-------\n");
memset(b, 0, sizeof(b));
}
void dfs2(int x, int fa) {
w[x]=1;
for(auto t : T1.G[x]) if(t.y!=fa && !b[t.y]) {
int y = t.y, z = t.z;
dfs2(y, x); w[x]+=w[y];
if(mx[y] < mx[rt]) rt=y, ST=x, ED=y, C=z;
}
mx[x]=max(w[x], sum-w[x]);
}
void dfs3(int x, int fa, int o) {
O[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;
dfs3(y, x, o);
}
}
vector<Point>h;
void solve(int x, vector<int>a) {
dfs2(x, 0); sum=w[x]; rt=0; dfs2(x, 0);
if(sum == 1) return ;
int st = ST, ed = ED; c = C;
debug("{%lld}%lld [%lld %lld] %lld\n", x, sum, st, ed, c);
vector<int>va, vb;
dis[st]=dis[ed]=0;
dfs3(st, ed, 1); dfs3(ed, st, 2);
h.clear();
for(auto u : a) {
h.pb({u, dis[u]+T1.dep[u], O[u]});
debug("%lld : %lld | %lld\n", u, dis[u]+T1.dep[u], O[u]);
if(O[u] == 1) va.pb(u);
if(O[u] == 2) vb.pb(u);
}
a.clear();
if(va.size() && vb.size()) Tree2 :: solve(h);
b[ed]=1; solve(st, va); b[ed]=0; va.clear();
b[st]=1; solve(ed, vb); b[st]=0; vb.clear();
}
void Main() {
vector<int>a;
for(i=1; i<=n; ++i) a.pb(i);
sort(a.begin(), a.end(), [] (int x, int y) { return T2.dfn[x] < T2.dfn[y]; });
for(auto t : a) debug("%d ", t); debug("\n");
solve(1, a);
}
}


signed main()
{
#ifdef LOCALd
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// T=read();
// while(T--) {
//
// }
n=read();
Tree1::Build();
Tree2::Read();
Tree1::Main();
assert(ans%2==0);
ans/=2;
for(i=1; i<=n; ++i) ans=max(ans, (2*T1.dep[i]-(T1.dep[i]+T2.dep[i])));
printf("%lld", ans);
return 0;
}