//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include "factories.h"
#define ll long long
const int nmax = 5e5 + 5, N = 1e6;
const ll oo = 1e18 + 5, base = 311;
const int lg = 18, tx = 26;
const ll mod = 998244353;
#define pii pair<ll, ll>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
using namespace std;
int n, q;
vector<pii> adj[nmax];
int h[nmax], up[nmax][lg + 1];
ll d[nmax];
void dfs_1(int u, int p){
for(auto [w, v] : adj[u]){
if(v == p) continue;
h[v] = h[u] + 1;
up[v][0] = u;
d[v] = d[u] + w;
for(int j = 1; j <= lg; ++j) up[v][j] = up[up[v][j - 1]][j - 1];
dfs_1(v, u);
}
}
int lca(int u, int v){
if(h[u] != h[v]){
if(h[u] < h[v]) swap(u, v);
int k = h[u] - h[v];
for(int j = 0; j <= __lg(k); ++j){
if(k >> j & 1) u = up[u][j];
}
}
if(u == v) return u;
for(int j = __lg(h[u]); j >=0;--j){
if(up[u][j] != up[v][j]){
u = up[u][j];
v = up[v][j];
}
}
return up[u][0];
}
ll dist(int u, int v){
return d[u] + d[v] - 2 * d[lca(u, v)];
}
bool vis[nmax];
int sz[nmax], par[nmax];
void dfs_2(int u, int p){
sz[u] = 1;
for(auto [w, v] : adj[u]){
if(v == p || vis[v]) continue;
dfs_2(v, u);
sz[u] += sz[v];
}
}
int cntw = 0;
int Find(int u, int p){
for(auto [w,v] : adj[u]){
if(v == p || vis[v]) continue;
if(sz[v] > cntw / 2) return Find(v, u);
}
return u;
}
int centroid(int u){
dfs_2(u, -1); cntw = sz[u];
int root = Find(u, -1);
vis[root] = 1;
for(auto [w, v] : adj[root]){
if(vis[v]) continue;
int x = centroid(v);
par[x] = root;
}
return root;
}
vector<int> rev;
ll dp[nmax];
void update(int u){
int x = u;
while(u > 0){
dp[u] = min(dp[u], dist(u, x));
rev.push_back(u);
u = par[u];
}
}
ll get(int u){
int x = u;
ll res = oo;
while(u > 0){
res = min(res, dp[u] + dist(u, x));
u = par[u];
}
return res;
}
int X[nmax], Y[nmax], S, T;
void Init(int n, int A[], int B[], int D[]){
for(int i = 0; i < n - 1; ++i){
int u, v, w;
u = A[i], v = B[i], w = D[i];
++u, ++v;
adj[u].push_back({w, v});
adj[v].push_back({w, u});
}
dfs_1(1, -1);
centroid(1);
memset(dp, 0x3f, sizeof dp);
}
ll Query(int S, int X[], int T, int Y[]){
for(int i = 0; i < S; ++i) ++X[i];
for(int i = 0; i < T; ++i) ++Y[i];
for(int i = 0; i < S; ++i) update(X[i]);
ll ans = oo;
for(int i = 0; i < T; ++i){
ans = min(ans,get(Y[i]));
}
for(auto p : rev)dp[p] = oo;
rev.clear();
return ans;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
41308 KB |
Output is correct |
2 |
Correct |
250 ms |
45800 KB |
Output is correct |
3 |
Correct |
639 ms |
55376 KB |
Output is correct |
4 |
Correct |
580 ms |
55640 KB |
Output is correct |
5 |
Correct |
691 ms |
55940 KB |
Output is correct |
6 |
Correct |
120 ms |
55376 KB |
Output is correct |
7 |
Correct |
611 ms |
55392 KB |
Output is correct |
8 |
Correct |
655 ms |
55452 KB |
Output is correct |
9 |
Correct |
679 ms |
55952 KB |
Output is correct |
10 |
Correct |
137 ms |
55376 KB |
Output is correct |
11 |
Correct |
630 ms |
55380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
41304 KB |
Output is correct |
2 |
Correct |
829 ms |
114164 KB |
Output is correct |
3 |
Correct |
1926 ms |
137300 KB |
Output is correct |
4 |
Correct |
339 ms |
129968 KB |
Output is correct |
5 |
Correct |
2671 ms |
167172 KB |
Output is correct |
6 |
Correct |
2037 ms |
137804 KB |
Output is correct |
7 |
Correct |
1732 ms |
75088 KB |
Output is correct |
8 |
Correct |
187 ms |
75716 KB |
Output is correct |
9 |
Correct |
1626 ms |
79188 KB |
Output is correct |
10 |
Correct |
1719 ms |
75752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
41308 KB |
Output is correct |
2 |
Correct |
250 ms |
45800 KB |
Output is correct |
3 |
Correct |
639 ms |
55376 KB |
Output is correct |
4 |
Correct |
580 ms |
55640 KB |
Output is correct |
5 |
Correct |
691 ms |
55940 KB |
Output is correct |
6 |
Correct |
120 ms |
55376 KB |
Output is correct |
7 |
Correct |
611 ms |
55392 KB |
Output is correct |
8 |
Correct |
655 ms |
55452 KB |
Output is correct |
9 |
Correct |
679 ms |
55952 KB |
Output is correct |
10 |
Correct |
137 ms |
55376 KB |
Output is correct |
11 |
Correct |
630 ms |
55380 KB |
Output is correct |
12 |
Correct |
5 ms |
41304 KB |
Output is correct |
13 |
Correct |
829 ms |
114164 KB |
Output is correct |
14 |
Correct |
1926 ms |
137300 KB |
Output is correct |
15 |
Correct |
339 ms |
129968 KB |
Output is correct |
16 |
Correct |
2671 ms |
167172 KB |
Output is correct |
17 |
Correct |
2037 ms |
137804 KB |
Output is correct |
18 |
Correct |
1732 ms |
75088 KB |
Output is correct |
19 |
Correct |
187 ms |
75716 KB |
Output is correct |
20 |
Correct |
1626 ms |
79188 KB |
Output is correct |
21 |
Correct |
1719 ms |
75752 KB |
Output is correct |
22 |
Correct |
1084 ms |
139460 KB |
Output is correct |
23 |
Correct |
1173 ms |
143064 KB |
Output is correct |
24 |
Correct |
3260 ms |
146488 KB |
Output is correct |
25 |
Correct |
3517 ms |
148648 KB |
Output is correct |
26 |
Correct |
3176 ms |
142220 KB |
Output is correct |
27 |
Correct |
4005 ms |
169404 KB |
Output is correct |
28 |
Correct |
434 ms |
137388 KB |
Output is correct |
29 |
Correct |
3166 ms |
141904 KB |
Output is correct |
30 |
Correct |
3128 ms |
141140 KB |
Output is correct |
31 |
Correct |
3233 ms |
141944 KB |
Output is correct |
32 |
Correct |
1627 ms |
84416 KB |
Output is correct |
33 |
Correct |
185 ms |
75068 KB |
Output is correct |
34 |
Correct |
506 ms |
73440 KB |
Output is correct |
35 |
Correct |
494 ms |
73316 KB |
Output is correct |
36 |
Correct |
1752 ms |
76116 KB |
Output is correct |
37 |
Correct |
1727 ms |
75856 KB |
Output is correct |