#include"factories.h"
#include<bits/stdc++.h>
#define fo(i,d,c) for(int i=d;i<=c;i++)
#define fod(i,c,d) for(int i=c;i>=d;i--)
#define maxn 1000010
#define fi first
#define se second
#define pb emplace_back
#define inf 1000000000000000
#define pii pair<int,int>
#define vii vector<pii>
#define vi vector<int>
using namespace std;
vector<pair<long long,long long>> ke[maxn];
long long minn[maxn];
bool is_remove[maxn];
long long sz[maxn];
vector<pair<long long,long long>> par[maxn];
long long dfs_size(long long u,long long parent = -1)
{
sz[u] = 1;
for(auto [v,_] : ke[u])
{
if(v == parent or is_remove[v]) continue;
sz[u] += dfs_size(v,u);
}
return sz[u];
}
long long dfs_centroid(long long u,long long tree_size,long long parent = -1)
{
for(auto [v,_] : ke[u])
{
if(v == parent or is_remove[v]) continue;
if(2 * sz[v] > tree_size) return dfs_centroid(v,tree_size,u);
}
return u;
}
void dfs_dist(long long u,long long parent,long long centroid,long long cur_len)
{
for(auto [v,w] : ke[u])
{
if(v == parent or is_remove[v]) continue;
dfs_dist(v,u,centroid,cur_len + w);
}
par[u].pb(cur_len,centroid);
}
void build_centroid(long long u)
{
long long centroid = dfs_centroid(u,dfs_size(u));
is_remove[centroid] = 1;
// do sth
for(auto [v,w] : ke[centroid])
{
if(is_remove[v]) continue;
dfs_dist(v,centroid,centroid,w);
}
for(auto [v,w] : ke[centroid])
{
if(is_remove[v]) continue;
build_centroid(v);
}
}
void update(long long u)
{
minn[u] = 0;
for(auto [dist,centroid] : par[u])
{
minn[centroid] = min(minn[centroid],dist);
}
}
void del(long long u)
{
minn[u] = inf;
for(auto [_,centroid] : par[u])
{
minn[centroid] = inf;
}
}
long long get(long long u)
{
long long ans = minn[u];
for(auto [dist,centroid] : par[u])
{
ans = min(ans,dist + minn[centroid]);
}
return ans;
}
void Init(int N, int A[], int B[], int D[])
{
fo(i,0,N - 2)
{
ke[A[i]].pb(B[i],D[i]);
ke[B[i]].pb(A[i],D[i]);
}
fo(i,0,N - 1) minn[i] = inf;
build_centroid(0);
}
long long Query(int S, int X[], int T, int Y[])
{
fo(i,0,S - 1) update(X[i]);
long long res = 1e18;
fo(i,0,T - 1) res = min(res,get(Y[i]));
fo(i,0,S - 1) del(X[i]);
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
68440 KB |
Output is correct |
2 |
Correct |
205 ms |
82808 KB |
Output is correct |
3 |
Correct |
229 ms |
83280 KB |
Output is correct |
4 |
Correct |
230 ms |
83384 KB |
Output is correct |
5 |
Correct |
228 ms |
83728 KB |
Output is correct |
6 |
Correct |
169 ms |
82104 KB |
Output is correct |
7 |
Correct |
218 ms |
83028 KB |
Output is correct |
8 |
Correct |
218 ms |
83284 KB |
Output is correct |
9 |
Correct |
239 ms |
83728 KB |
Output is correct |
10 |
Correct |
156 ms |
82096 KB |
Output is correct |
11 |
Correct |
238 ms |
83208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
68184 KB |
Output is correct |
2 |
Correct |
1891 ms |
244652 KB |
Output is correct |
3 |
Correct |
2675 ms |
290424 KB |
Output is correct |
4 |
Correct |
681 ms |
140736 KB |
Output is correct |
5 |
Correct |
3559 ms |
383428 KB |
Output is correct |
6 |
Correct |
3176 ms |
290712 KB |
Output is correct |
7 |
Correct |
780 ms |
122140 KB |
Output is correct |
8 |
Correct |
244 ms |
97060 KB |
Output is correct |
9 |
Correct |
857 ms |
126812 KB |
Output is correct |
10 |
Correct |
867 ms |
122592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
68440 KB |
Output is correct |
2 |
Correct |
205 ms |
82808 KB |
Output is correct |
3 |
Correct |
229 ms |
83280 KB |
Output is correct |
4 |
Correct |
230 ms |
83384 KB |
Output is correct |
5 |
Correct |
228 ms |
83728 KB |
Output is correct |
6 |
Correct |
169 ms |
82104 KB |
Output is correct |
7 |
Correct |
218 ms |
83028 KB |
Output is correct |
8 |
Correct |
218 ms |
83284 KB |
Output is correct |
9 |
Correct |
239 ms |
83728 KB |
Output is correct |
10 |
Correct |
156 ms |
82096 KB |
Output is correct |
11 |
Correct |
238 ms |
83208 KB |
Output is correct |
12 |
Correct |
14 ms |
68184 KB |
Output is correct |
13 |
Correct |
1891 ms |
244652 KB |
Output is correct |
14 |
Correct |
2675 ms |
290424 KB |
Output is correct |
15 |
Correct |
681 ms |
140736 KB |
Output is correct |
16 |
Correct |
3559 ms |
383428 KB |
Output is correct |
17 |
Correct |
3176 ms |
290712 KB |
Output is correct |
18 |
Correct |
780 ms |
122140 KB |
Output is correct |
19 |
Correct |
244 ms |
97060 KB |
Output is correct |
20 |
Correct |
857 ms |
126812 KB |
Output is correct |
21 |
Correct |
867 ms |
122592 KB |
Output is correct |
22 |
Correct |
2259 ms |
246252 KB |
Output is correct |
23 |
Correct |
2234 ms |
251780 KB |
Output is correct |
24 |
Correct |
3431 ms |
294612 KB |
Output is correct |
25 |
Correct |
3468 ms |
297160 KB |
Output is correct |
26 |
Correct |
3174 ms |
295992 KB |
Output is correct |
27 |
Correct |
4043 ms |
386116 KB |
Output is correct |
28 |
Correct |
676 ms |
146916 KB |
Output is correct |
29 |
Correct |
2992 ms |
295252 KB |
Output is correct |
30 |
Correct |
3447 ms |
295364 KB |
Output is correct |
31 |
Correct |
3270 ms |
295416 KB |
Output is correct |
32 |
Correct |
860 ms |
126960 KB |
Output is correct |
33 |
Correct |
238 ms |
97164 KB |
Output is correct |
34 |
Correct |
512 ms |
112516 KB |
Output is correct |
35 |
Correct |
554 ms |
113492 KB |
Output is correct |
36 |
Correct |
708 ms |
121308 KB |
Output is correct |
37 |
Correct |
699 ms |
121172 KB |
Output is correct |