#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define F first
// #define S second
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int, int>
#define vpi vector<pi>
#define vb vector<bool>
#define vvb vector<vb>
#define pb push_back
#define ppb pop_back
#define read(a) for(auto &x:a) cin >> x;
#define print(a) for(auto x:a) cout << x << " "; cout << "\n";
#define vc vector<char>
#define vvc vector<vc>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define double ld
// #define int long long
const long long INF = 4e18;
int n;
vector<vpi> adj;
vector<long long> d, dc, sub, par, best, vis;
vvi up;
vb del;
int tt = 0;
int lca(int a, int b){
if(d[a] > d[b]) swap(a, b);
for(int j=0; j<=20; j++)
if((1LL<<j)&(d[b]-d[a])) b = up[b][j];
if(a == b) return a;
for(int j=20; j>=0; j--)
if(up[a][j] != up[b][j])
a = up[a][j], b = up[b][j];
return up[a][0];
}
long long dist(int a, int b){
return dc[a]+dc[b]-2*dc[lca(a, b)];
}
void build(){
d = dc = sub = par = vis = best = vector<long long>(n+1);
up = vvi(n+1, vi(21));
del = vb(n+1);
function<void(int, int)> dfs0 = [&](int s, int e){
for(auto [u, c]:adj[s]){
if(u == e) continue;
d[u] = d[s]+1;
dc[u] = dc[s]+c;
up[u][0] = s;
dfs0(u, s);
}
};
dfs0(1, 0);
for(int j=1; j<=20; j++)
for(int i=1; i<=n; i++)
up[i][j] = up[up[i][j-1]][j-1];
//START_CENTROID
function<void(int, int)> dfs_sub = [&](int s, int e){
sub[s] = 1;
for(auto [u, c]:adj[s]){
if(u == e || del[u]) continue;
dfs_sub(u, s);
sub[s] += sub[u];
}
};
function<int(int, int, int)> centroid = [&](int s, int e, int sz){
for(auto [u, c]:adj[s])
if(u != e && !del[u] && sub[u] > sz/2) return centroid(u, s, sz);
return s;
};
function<void(int, int)> decompose = [&](int s, int p){
dfs_sub(s, 0);
int C = centroid(s, 0, sub[s]);
par[C] = p;
del[C] = 1;
for(auto [u, c]:adj[C])
if(!del[u]) decompose(u, C);
};
decompose(1, -1);
//END_CENTROID
}
void Init(int N, int A[], int B[], int D[]){
n = N;
adj = vector<vpi>(n+1);
for(int i=0; i<n-1; i++){
adj[A[i]+1].pb({B[i]+1, D[i]});
adj[B[i]+1].pb({A[i]+1, D[i]});
}
build();
}
long long Query(int S, int X[], int T, int Y[]){
tt++;
long long ans = INF;
int x = 0, y = 0;
for(int i=0; i<T; i++){
Y[i]++;
int curr = Y[i];
while(curr != -1){
if(vis[curr] < tt){
vis[curr] = tt;
best[curr] = INF;
}
best[curr] = min(best[curr], dist(curr, Y[i]));
curr = par[curr];
++x;
}
}
for(int i=0; i<S; i++){
X[i]++;
int curr = X[i];
while(curr != -1){
if(vis[curr] < tt){
vis[curr] = tt;
best[curr] = INF;
}
ans = min(ans, best[curr]+dist(curr, X[i]));
curr = par[curr];
++y;
}
}
// if(max(x, y) > 100) assert(false);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
852 KB |
Output is correct |
2 |
Correct |
930 ms |
9788 KB |
Output is correct |
3 |
Correct |
1828 ms |
10060 KB |
Output is correct |
4 |
Correct |
1873 ms |
9836 KB |
Output is correct |
5 |
Correct |
2496 ms |
10176 KB |
Output is correct |
6 |
Correct |
329 ms |
9728 KB |
Output is correct |
7 |
Correct |
1805 ms |
9924 KB |
Output is correct |
8 |
Correct |
1893 ms |
9968 KB |
Output is correct |
9 |
Correct |
2495 ms |
10056 KB |
Output is correct |
10 |
Correct |
325 ms |
9740 KB |
Output is correct |
11 |
Correct |
1810 ms |
10064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
468 KB |
Output is correct |
2 |
Correct |
4021 ms |
125836 KB |
Output is correct |
3 |
Execution timed out |
8032 ms |
128636 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
852 KB |
Output is correct |
2 |
Correct |
930 ms |
9788 KB |
Output is correct |
3 |
Correct |
1828 ms |
10060 KB |
Output is correct |
4 |
Correct |
1873 ms |
9836 KB |
Output is correct |
5 |
Correct |
2496 ms |
10176 KB |
Output is correct |
6 |
Correct |
329 ms |
9728 KB |
Output is correct |
7 |
Correct |
1805 ms |
9924 KB |
Output is correct |
8 |
Correct |
1893 ms |
9968 KB |
Output is correct |
9 |
Correct |
2495 ms |
10056 KB |
Output is correct |
10 |
Correct |
325 ms |
9740 KB |
Output is correct |
11 |
Correct |
1810 ms |
10064 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
4021 ms |
125836 KB |
Output is correct |
14 |
Execution timed out |
8032 ms |
128636 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |