#include<bits/stdc++.h>
#define pli pair<int , long long>
#include "factories.h"
using namespace std;
const int MAX = 5e5 + 1 , LOG = 21;
const long long oo = 1e16;
int cen , dead[MAX] , sz[MAX];
long long e[MAX];
vector<int> g[MAX];
vector<long long> w[MAX];
vector<pli> ups[MAX];
int dfs(int u , int p = -1){
sz[u] = 1;
for(int x = 0;x < (int)g[u].size();x ++){
if(g[u][x] != p && !dead[g[u][x]]){
dfs(g[u][x] , u);
sz[u] += sz[g[u][x]];
}
}
return sz[u];
}
int centroid(int u , int e , int p = -1){
for(int x = 0;x < (int)g[u].size();x ++){
if(g[u][x] != p && !dead[g[u][x]] && sz[g[u][x]] > e / 2){
return centroid(g[u][x] , e , u);
}
}
return u;
}
void build(int u , int p , long long D){
ups[u].push_back({cen , D});
for(int x = 0;x < (int)g[u].size();x ++){
if(g[u][x] != p && !dead[g[u][x]]){
build(g[u][x] , u , D + w[u][x]);
}
}
}
void solve(int u){
u = centroid(u , dfs(u));
cen = u;
dead[u] = 1;
for(int x = 0;x < (int)g[u].size();x ++){
if(!dead[g[u][x]]){
build(g[u][x] , u , w[u][x]);
}
}
for(int x = 0;x < (int)g[u].size();x ++){
if(!dead[g[u][x]]){
solve(g[u][x]);
}
}
}
void Init(int N , int A[] , int B[] , int D[]){
for(int i = 0;i < N - 1;i ++){
++A[i] , ++B[i];
g[A[i]].push_back(B[i]);
w[A[i]].push_back(D[i]);
g[B[i]].push_back(A[i]);
w[B[i]].push_back(D[i]);
}
for(int i = 1;i <= N;i ++){
dead[i] = 0;
e[i] = oo;
}
solve(1);
}
long long Query(int S , int X[] , int T , int Y[]){
long long ans = oo;
for(int i = 0;i < T;i ++){
++Y[i];
e[Y[i]] = 0;
for(auto [u , D] : ups[Y[i]]){
e[u] = min(e[u] , D);
}
}
for(int i = 0;i < S;i ++){
++X[i];
ans = min(ans , e[X[i]]);
for(auto [u , D] : ups[X[i]]){
ans = min(ans , e[u] + D);
}
}
for(int i = 0;i < T;i ++){
e[Y[i]] = oo;
for(auto [u , D] : ups[Y[i]]){
e[u] = oo;
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
62300 KB |
Output is correct |
2 |
Correct |
148 ms |
69924 KB |
Output is correct |
3 |
Correct |
163 ms |
73040 KB |
Output is correct |
4 |
Correct |
164 ms |
73044 KB |
Output is correct |
5 |
Correct |
181 ms |
73556 KB |
Output is correct |
6 |
Correct |
110 ms |
72020 KB |
Output is correct |
7 |
Correct |
163 ms |
73040 KB |
Output is correct |
8 |
Correct |
170 ms |
72996 KB |
Output is correct |
9 |
Correct |
185 ms |
73556 KB |
Output is correct |
10 |
Correct |
127 ms |
72160 KB |
Output is correct |
11 |
Correct |
190 ms |
72920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
62296 KB |
Output is correct |
2 |
Correct |
1313 ms |
228364 KB |
Output is correct |
3 |
Correct |
1980 ms |
277956 KB |
Output is correct |
4 |
Correct |
437 ms |
133580 KB |
Output is correct |
5 |
Correct |
2426 ms |
374480 KB |
Output is correct |
6 |
Correct |
1964 ms |
277928 KB |
Output is correct |
7 |
Correct |
427 ms |
110168 KB |
Output is correct |
8 |
Correct |
191 ms |
86336 KB |
Output is correct |
9 |
Correct |
444 ms |
114256 KB |
Output is correct |
10 |
Correct |
429 ms |
110676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
62300 KB |
Output is correct |
2 |
Correct |
148 ms |
69924 KB |
Output is correct |
3 |
Correct |
163 ms |
73040 KB |
Output is correct |
4 |
Correct |
164 ms |
73044 KB |
Output is correct |
5 |
Correct |
181 ms |
73556 KB |
Output is correct |
6 |
Correct |
110 ms |
72020 KB |
Output is correct |
7 |
Correct |
163 ms |
73040 KB |
Output is correct |
8 |
Correct |
170 ms |
72996 KB |
Output is correct |
9 |
Correct |
185 ms |
73556 KB |
Output is correct |
10 |
Correct |
127 ms |
72160 KB |
Output is correct |
11 |
Correct |
190 ms |
72920 KB |
Output is correct |
12 |
Correct |
8 ms |
62296 KB |
Output is correct |
13 |
Correct |
1313 ms |
228364 KB |
Output is correct |
14 |
Correct |
1980 ms |
277956 KB |
Output is correct |
15 |
Correct |
437 ms |
133580 KB |
Output is correct |
16 |
Correct |
2426 ms |
374480 KB |
Output is correct |
17 |
Correct |
1964 ms |
277928 KB |
Output is correct |
18 |
Correct |
427 ms |
110168 KB |
Output is correct |
19 |
Correct |
191 ms |
86336 KB |
Output is correct |
20 |
Correct |
444 ms |
114256 KB |
Output is correct |
21 |
Correct |
429 ms |
110676 KB |
Output is correct |
22 |
Correct |
1447 ms |
233276 KB |
Output is correct |
23 |
Correct |
1468 ms |
238804 KB |
Output is correct |
24 |
Correct |
2229 ms |
280124 KB |
Output is correct |
25 |
Correct |
2217 ms |
282960 KB |
Output is correct |
26 |
Correct |
2107 ms |
281584 KB |
Output is correct |
27 |
Correct |
2779 ms |
372160 KB |
Output is correct |
28 |
Correct |
526 ms |
138356 KB |
Output is correct |
29 |
Correct |
2096 ms |
280740 KB |
Output is correct |
30 |
Correct |
2126 ms |
280148 KB |
Output is correct |
31 |
Correct |
2141 ms |
281000 KB |
Output is correct |
32 |
Correct |
498 ms |
114508 KB |
Output is correct |
33 |
Correct |
175 ms |
86336 KB |
Output is correct |
34 |
Correct |
308 ms |
101200 KB |
Output is correct |
35 |
Correct |
315 ms |
101792 KB |
Output is correct |
36 |
Correct |
402 ms |
109196 KB |
Output is correct |
37 |
Correct |
416 ms |
109308 KB |
Output is correct |