/* Bismillahir Rahmanir Rahim */
#include <bits/stdc++.h>
#include "factories.h"
using namespace std;
typedef pair <int, long long> pii;
const int N = 500005;
const long long oo = 1e18;
vector <pii> g[N];
int n;
int tot;
int sub[N];
int anc[N];
int vis[N];
int lst[N], t;
int pa[N][20];
long long dep[N];
long long level[N];
long long mn[N];
long long pre[N][25];
stack <int> rem;
void dfs(int at, int par){
pa[at][0] = par;
for(int i = 1; i <= 18; ++i) pa[at][i] = pa[ pa[at][i-1] ][ i-1 ];
for(auto u : g[at]){
if(u.first != par){
dep[u.first] = dep[at] + u.second;
level[u.first] = level[at] + 1;
dfs(u.first, at);
}
}
}
int lca(int a, int b){
if(level[a] < level[b]) swap(a, b);
for(int i = 18; i >= 0; --i) if(level[a] - (1 << i) >= level[b]) a = pa[a][i];
if(a == b) return a;
for(int i = 18; i >= 0; --i) if(pa[a][i] != pa[b][i]) a = pa[a][i], b = pa[b][i];
return pa[a][0];
}
long long dis(int a, int b){
int lc = lca(a, b);
return dep[a] + dep[b] - 2LL * dep[lc];
}
void dfs0(int at, int par){
++tot;
sub[at] = 1;
for(auto u : g[at]){
if(vis[u.first] || u.first == par) continue;
dfs0(u.first, at);
sub[at] += sub[u.first];
}
}
int get(int at, int par){
for(auto u : g[at]){
if(vis[u.first] || u.first == par) continue;
if(sub[u.first] > tot / 2) return get(u.first, at);
}
return at;
}
void decompose(int at, int par){
tot = 0;
dfs0(at, at);
int cen = get(at, at);
vis[cen] = 1;
if(par){
anc[cen] = par;
}
for(auto u : g[cen]){
if(vis[u.first]) continue;
decompose(u.first, cen);
}
}
void update(int from){
int cur = from, id = 0;
while(cur){
mn[cur] = min(mn[cur], pre[from][++id]);
if(lst[cur] != t){
rem.push(cur);
lst[cur] = t;
}
cur = anc[cur];
}
}
long long query(int from){
int cur = from, id = 0;
long long ret = oo;
while(cur){
ret = min(ret, pre[from][++id] + mn[cur]);
cur = anc[cur];
}
return ret;
}
void reset(){
while(!rem.empty()){
mn[rem.top()] = oo;
rem.pop();
}
++t;
}
long long Query(int S, int X[], int T, int Y[]){
reset();
for(int i = 0; i < S; ++i){
update(X[i] + 1);
}
long long ret = oo;
for(int i = 0; i < T; ++i){
ret = min(ret, query(Y[i] + 1));
}
return ret;
}
void Init(int N, int A[], int B[], int D[]){
n = N;
for(int i = 0; i < N - 1; ++i){
g[A[i] + 1].push_back({B[i] + 1, D[i]});
g[B[i] + 1].push_back({A[i] + 1, D[i]});
}
dfs(1, 0);
decompose(1, 0);
for(int i = 1; i <= n; ++i) mn[i] = oo;
for(int i = 1; i <= n; ++i){
int cur = i, id = 0;
while(cur){
pre[i][++id] = dis(i, cur);
cur = anc[cur];
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
192264 KB |
Output is correct |
2 |
Correct |
396 ms |
192528 KB |
Output is correct |
3 |
Correct |
433 ms |
192528 KB |
Output is correct |
4 |
Correct |
459 ms |
192528 KB |
Output is correct |
5 |
Correct |
396 ms |
192684 KB |
Output is correct |
6 |
Correct |
346 ms |
192552 KB |
Output is correct |
7 |
Correct |
386 ms |
192528 KB |
Output is correct |
8 |
Correct |
356 ms |
192528 KB |
Output is correct |
9 |
Correct |
443 ms |
192676 KB |
Output is correct |
10 |
Correct |
359 ms |
192552 KB |
Output is correct |
11 |
Correct |
403 ms |
192528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
192264 KB |
Output is correct |
2 |
Correct |
2893 ms |
217872 KB |
Output is correct |
3 |
Execution timed out |
6000 ms |
222312 KB |
Execution timed out |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3183 ms |
218360 KB |
Output is correct |
2 |
Correct |
3189 ms |
218588 KB |
Output is correct |
3 |
Execution timed out |
6000 ms |
221924 KB |
Execution timed out |
4 |
Halted |
0 ms |
0 KB |
- |