#include "factories.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,n) for(int i = 0; i < n; i ++)
#define FOR(i,a,b) for(int i = a; i < b; i ++)
#define pii pair<int,int>
#define F first
#define S second
#define vi vector<int>
#define pb push_back
#define runtime() ((double)(clock())/CLOCKS_PER_SEC)
int n,centroid;
const int MAXN = 500005;
const ll INF = 1e16;
int par[MAXN][21],cenpar[MAXN];
int dep[MAXN],sz[MAXN];
vector<pii> adj[MAXN];
bitset<MAXN> vis;
vi g[MAXN];
ll mnd[MAXN];
ll cendist[MAXN][21];
int cendep[MAXN];
void dfs0(int u){
for(auto v:adj[u]){
if(v.F != par[u][0]){
par[v.F][0] = u;
dep[v.F] = dep[u]+1;
dfs0(v.F);
}
}
}
void dfs_sz(int u,int p){
sz[u] = 1;
for(auto v:adj[u]){
if(v.F != p and vis[v.F] == 0){
dfs_sz(v.F,u);
sz[u] += sz[v.F];
}
}
}
int find_centroid(int u,int p,int tot){
for(auto v:adj[u]){
if(v.F == p or vis[v.F]) continue;
if(sz[v.F] > tot/2) return find_centroid(v.F,u,tot);
}
return u;
}
void dfs1(int u,int p,ll dd,int st){
cendist[u][cendep[u]-cendep[st]] = dd;
for(auto v:adj[u]){
if(v.F != p and vis[v.F] == 0){
dfs1(v.F,u,dd+v.S,st);
}
}
}
void centroid_decomposition(int u,int p){
dfs_sz(u,u);
int cen = find_centroid(u,p,sz[u]);
if(p == -1){
centroid = cen;
cendep[cen] = 0;
}
else{
g[p].pb(cen);
cendep[cen] = cendep[p]+1;
}
cenpar[cen] = p;
vis[cen] = 1;
for(auto v:adj[cen]){
if(!vis[v.F]) centroid_decomposition(v.F,cen);
}
}
void calc_dist(int u){
dfs1(u,u,0,u);
vis[u] = 1;
for(auto v:g[u]) calc_dist(v);
}
void Init(int N, int A[], int B[], int D[]){
n = N;
REP(i,n-1){
adj[A[i]].pb({B[i],D[i]});
adj[B[i]].pb({A[i],D[i]});
}
par[0][0] = 0;
dep[0] = 0;
dfs0(0);
FOR(j,1,21){
REP(i,n){
par[i][j] = par[par[i][j-1]][j-1];
}
}
centroid_decomposition(0,-1);
vis.reset();
calc_dist(centroid);
REP(i,n+1) mnd[i] = INF;
}
void upd(int u,int type){
int cur = u;
int cnt = 0;
while(cur != -1){
if(type == 0){
mnd[cur] = min(mnd[cur],cendist[u][cnt]);
}
else mnd[cur] = INF;
cur = cenpar[cur];
cnt++;
}
}
ll quer(int u){
ll res = INF;
int cur = u;
int cnt = 0;
while(cur != -1){
res = min(res,cendist[u][cnt]+mnd[cur]);
cur = cenpar[cur];
cnt ++;
}
return res;
}
ll Query(int S, int X[], int T, int Y[]) {
REP(i,S) upd(X[i],0);
ll ans = INF;
REP(i,T) ans = min(ans,quer(Y[i]));
REP(i,S) upd(X[i],1);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
24576 KB |
Output is correct |
2 |
Correct |
372 ms |
37908 KB |
Output is correct |
3 |
Correct |
399 ms |
38008 KB |
Output is correct |
4 |
Correct |
401 ms |
38008 KB |
Output is correct |
5 |
Correct |
428 ms |
38136 KB |
Output is correct |
6 |
Correct |
296 ms |
38012 KB |
Output is correct |
7 |
Correct |
397 ms |
38008 KB |
Output is correct |
8 |
Correct |
401 ms |
38068 KB |
Output is correct |
9 |
Correct |
420 ms |
38264 KB |
Output is correct |
10 |
Correct |
336 ms |
38008 KB |
Output is correct |
11 |
Correct |
401 ms |
38136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
24192 KB |
Output is correct |
2 |
Correct |
2512 ms |
197864 KB |
Output is correct |
3 |
Correct |
3691 ms |
199628 KB |
Output is correct |
4 |
Correct |
1033 ms |
195424 KB |
Output is correct |
5 |
Correct |
4665 ms |
223120 KB |
Output is correct |
6 |
Correct |
3743 ms |
200824 KB |
Output is correct |
7 |
Correct |
1055 ms |
71800 KB |
Output is correct |
8 |
Correct |
528 ms |
71784 KB |
Output is correct |
9 |
Correct |
1229 ms |
75736 KB |
Output is correct |
10 |
Correct |
1152 ms |
72952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
24576 KB |
Output is correct |
2 |
Correct |
372 ms |
37908 KB |
Output is correct |
3 |
Correct |
399 ms |
38008 KB |
Output is correct |
4 |
Correct |
401 ms |
38008 KB |
Output is correct |
5 |
Correct |
428 ms |
38136 KB |
Output is correct |
6 |
Correct |
296 ms |
38012 KB |
Output is correct |
7 |
Correct |
397 ms |
38008 KB |
Output is correct |
8 |
Correct |
401 ms |
38068 KB |
Output is correct |
9 |
Correct |
420 ms |
38264 KB |
Output is correct |
10 |
Correct |
336 ms |
38008 KB |
Output is correct |
11 |
Correct |
401 ms |
38136 KB |
Output is correct |
12 |
Correct |
17 ms |
24192 KB |
Output is correct |
13 |
Correct |
2512 ms |
197864 KB |
Output is correct |
14 |
Correct |
3691 ms |
199628 KB |
Output is correct |
15 |
Correct |
1033 ms |
195424 KB |
Output is correct |
16 |
Correct |
4665 ms |
223120 KB |
Output is correct |
17 |
Correct |
3743 ms |
200824 KB |
Output is correct |
18 |
Correct |
1055 ms |
71800 KB |
Output is correct |
19 |
Correct |
528 ms |
71784 KB |
Output is correct |
20 |
Correct |
1229 ms |
75736 KB |
Output is correct |
21 |
Correct |
1152 ms |
72952 KB |
Output is correct |
22 |
Correct |
2902 ms |
203432 KB |
Output is correct |
23 |
Correct |
3062 ms |
205924 KB |
Output is correct |
24 |
Correct |
4369 ms |
205652 KB |
Output is correct |
25 |
Correct |
4359 ms |
209028 KB |
Output is correct |
26 |
Correct |
4288 ms |
205736 KB |
Output is correct |
27 |
Correct |
5648 ms |
225272 KB |
Output is correct |
28 |
Correct |
1270 ms |
203712 KB |
Output is correct |
29 |
Correct |
4336 ms |
205432 KB |
Output is correct |
30 |
Correct |
4269 ms |
205132 KB |
Output is correct |
31 |
Correct |
4294 ms |
205320 KB |
Output is correct |
32 |
Correct |
1260 ms |
86104 KB |
Output is correct |
33 |
Correct |
533 ms |
81644 KB |
Output is correct |
34 |
Correct |
858 ms |
78908 KB |
Output is correct |
35 |
Correct |
858 ms |
78712 KB |
Output is correct |
36 |
Correct |
1072 ms |
79484 KB |
Output is correct |
37 |
Correct |
1082 ms |
79352 KB |
Output is correct |