#include <bits/stdc++.h>
#include "factories.h"
using namespace std;
//#define int long long
typedef long long ll;
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define fi first
#define se second
#define getchar_unlocked getchar_nolock
ll n, m, q;
vector<pi>v[500005];
vector<ll> ct[500005];
ll dep[500005], par[20][500005], sz[500005], vis[500005];
ll dist[500005][20];
ll mn[500005];
ll di[5000005];
void dfs(int x, int p, int d){
sz[x] = 1;
for(auto [i, j] : v[x]){
if(i == p || vis[i])continue;
dfs(i, x, d + 1);
sz[x] += sz[i];
}
}
int rt, lim;
int cent(int x, int p){
for(auto [i, j] : v[x]){
if(i == p || vis[i])continue;
if(sz[i] * 2 > lim)return cent(i, x);
}
return x;
}
int lvl = 1;
void dfs2(int x, int p, ll cur){
dist[x][lvl] = cur;
for(auto [i, j] : v[x]){
if(i == p || vis[i])continue;
dfs2(i, x, cur + j);
}
}
void proc(int x, int p){
par[0][x] = p;
dep[x] = (p == -1 ? 0 : dep[p]) + 1;
for(auto i : ct[x])if(i != p)proc(i, x);
}
void Init(int N, int A[], int B[], int D[]) {
for(int i=0;i<N-1;i++)A[i]++, B[i]++, v[A[i]].push_back({B[i], D[i]}), v[B[i]].push_back({A[i], D[i]});
dfs(1, -1, 1);
lim = N;
int x = cent(1, -1);
rt = x;
queue<pi>qu;
qu.push({x, 1});
//cerr << rt << '\n';
vis[x] = 1;
while(!qu.empty()){
x = qu.front().fi;
int y = qu.front().se; qu.pop();
vis[x] = 1;
lvl = y;
dfs2(x, -1, 0);
for(auto [i, j] : v[x]){
if(vis[i])continue;
dfs(i, x, 1);
lim = sz[i];
int lmao = cent(i, x);
ct[x].push_back(lmao);
ct[lmao].push_back(x);
//cerr << "ADDING EDGE " << x << " " << lmao << "\n";
qu.push({lmao, y + 1});
}
}
assert(lvl <= 20);
proc(rt, -1);
for(int i=1;i<=N;i++)mn[i] = 1e18;
}
long long Query(int S, int X[], int T, int Y[]) {
vector<int>tmp;
for(int i=0;i<S;i++)X[i]++;
for(int i=0;i<T;i++)Y[i]++;
for(int i=0;i<T;i++){
int ori = Y[i];
mn[ori] = 0;
while(ori != -1){
mn[ori] = min(mn[ori], dist[Y[i]][dep[ori]]);
tmp.push_back(ori);
ori = par[0][ori];
}
}
ll ans = 1e18;
for(int i=0;i<S;i++){
ll ans2 = mn[X[i]], ori = X[i];
while(ori != -1){
ans2 = min(ans2, mn[ori] + dist[X[i]][dep[ori]]);
ori = par[0][ori];
}
ans = min(ans, ans2);
}
for(auto i : tmp)mn[i] = 1e18;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24452 KB |
Output is correct |
2 |
Correct |
272 ms |
42824 KB |
Output is correct |
3 |
Correct |
318 ms |
42828 KB |
Output is correct |
4 |
Correct |
295 ms |
42768 KB |
Output is correct |
5 |
Correct |
319 ms |
43240 KB |
Output is correct |
6 |
Correct |
191 ms |
42996 KB |
Output is correct |
7 |
Correct |
294 ms |
42804 KB |
Output is correct |
8 |
Correct |
299 ms |
43316 KB |
Output is correct |
9 |
Correct |
318 ms |
43236 KB |
Output is correct |
10 |
Correct |
194 ms |
42876 KB |
Output is correct |
11 |
Correct |
291 ms |
42716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
24072 KB |
Output is correct |
2 |
Correct |
2343 ms |
198120 KB |
Output is correct |
3 |
Correct |
3578 ms |
200708 KB |
Output is correct |
4 |
Correct |
845 ms |
188556 KB |
Output is correct |
5 |
Correct |
4777 ms |
219436 KB |
Output is correct |
6 |
Correct |
3619 ms |
187108 KB |
Output is correct |
7 |
Correct |
891 ms |
77484 KB |
Output is correct |
8 |
Correct |
382 ms |
79188 KB |
Output is correct |
9 |
Correct |
1095 ms |
82496 KB |
Output is correct |
10 |
Correct |
977 ms |
78808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24452 KB |
Output is correct |
2 |
Correct |
272 ms |
42824 KB |
Output is correct |
3 |
Correct |
318 ms |
42828 KB |
Output is correct |
4 |
Correct |
295 ms |
42768 KB |
Output is correct |
5 |
Correct |
319 ms |
43240 KB |
Output is correct |
6 |
Correct |
191 ms |
42996 KB |
Output is correct |
7 |
Correct |
294 ms |
42804 KB |
Output is correct |
8 |
Correct |
299 ms |
43316 KB |
Output is correct |
9 |
Correct |
318 ms |
43236 KB |
Output is correct |
10 |
Correct |
194 ms |
42876 KB |
Output is correct |
11 |
Correct |
291 ms |
42716 KB |
Output is correct |
12 |
Correct |
15 ms |
24072 KB |
Output is correct |
13 |
Correct |
2343 ms |
198120 KB |
Output is correct |
14 |
Correct |
3578 ms |
200708 KB |
Output is correct |
15 |
Correct |
845 ms |
188556 KB |
Output is correct |
16 |
Correct |
4777 ms |
219436 KB |
Output is correct |
17 |
Correct |
3619 ms |
187108 KB |
Output is correct |
18 |
Correct |
891 ms |
77484 KB |
Output is correct |
19 |
Correct |
382 ms |
79188 KB |
Output is correct |
20 |
Correct |
1095 ms |
82496 KB |
Output is correct |
21 |
Correct |
977 ms |
78808 KB |
Output is correct |
22 |
Correct |
2625 ms |
209156 KB |
Output is correct |
23 |
Correct |
2664 ms |
208184 KB |
Output is correct |
24 |
Correct |
4164 ms |
215640 KB |
Output is correct |
25 |
Correct |
4232 ms |
217808 KB |
Output is correct |
26 |
Correct |
4059 ms |
209056 KB |
Output is correct |
27 |
Correct |
5487 ms |
243012 KB |
Output is correct |
28 |
Correct |
984 ms |
215016 KB |
Output is correct |
29 |
Correct |
4094 ms |
208476 KB |
Output is correct |
30 |
Correct |
4205 ms |
208208 KB |
Output is correct |
31 |
Correct |
4120 ms |
208568 KB |
Output is correct |
32 |
Correct |
1106 ms |
88484 KB |
Output is correct |
33 |
Correct |
389 ms |
80304 KB |
Output is correct |
34 |
Correct |
649 ms |
75172 KB |
Output is correct |
35 |
Correct |
683 ms |
75204 KB |
Output is correct |
36 |
Correct |
876 ms |
76064 KB |
Output is correct |
37 |
Correct |
848 ms |
75872 KB |
Output is correct |