#include "factories.h"
#include <bits/stdc++.h>
#define long long long
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 5e5+5;
int n;
vector<pii> g[N];
int par[N], dep[N], sz[N];
long d[N][20];
bitset<N> chk;
int get_sz(int u, int p) {
sz[u] = 1;
for(pii v : g[u]) if(v.x != p && !chk[v.x])
sz[u] += get_sz(v.x, u);
return sz[u];
}
int find_cen(int u, int p, int all, int &ret) {
int mx = all - sz[u];
for(pii v : g[u]) if(v.x != p && !chk[v.x])
mx = max(mx, find_cen(v.x, u, all, ret));
if(mx <= (all + 1) / 2) ret = u;
return sz[u];
}
void dfs(int u, int p, int i) {
for(pii v : g[u]) if(v.x != p && !chk[v.x]) {
d[v.x][i] = d[u][i] + v.y;
dfs(v.x, u, i);
}
}
void process(int u, int p) {
get_sz(u, u), find_cen(u, u, sz[u], u);
dep[u] = dep[p] + 1, par[u] = p, chk[u] = 1;
dfs(u, u, dep[u]);
for(pii v : g[u]) if(!chk[v.x]) process(v.x, u);
}
long dp[N];
void update(int x, bool fill) {
dp[x] = fill ? 0 : 1e18;
for(int u = par[x]; u; u = par[u]) {
if(fill) dp[u] = min(dp[u], d[x][dep[u]]);
else dp[u] = 1e18;
}
}
long query(int x) {
long ret = dp[x];
for(int u = par[x]; u; u = par[u])
ret = min(ret, dp[u] + d[x][dep[u]]);
return ret;
}
void Init(int _n, int A[], int B[], int D[]) {
n = _n;
for(int i = 0; i < n-1; i++) {
g[++A[i]].emplace_back(++B[i], D[i]);
g[B[i]].emplace_back(A[i], D[i]);
}
process(1, 0);
fill_n(dp, N, 1e18);
}
long Query(int S, int X[], int T, int Y[]) {
long ans = 1e18;
for(int i = 0; i < S; i++) update(++X[i], 1);
for(int i = 0; i < T; i++) ans = min(ans, query(++Y[i]));
for(int i = 0; i < S; i++) update(X[i], 0);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
16384 KB |
Output is correct |
2 |
Correct |
395 ms |
25096 KB |
Output is correct |
3 |
Correct |
453 ms |
25080 KB |
Output is correct |
4 |
Correct |
426 ms |
25208 KB |
Output is correct |
5 |
Correct |
441 ms |
25464 KB |
Output is correct |
6 |
Correct |
313 ms |
34680 KB |
Output is correct |
7 |
Correct |
426 ms |
34680 KB |
Output is correct |
8 |
Correct |
445 ms |
34680 KB |
Output is correct |
9 |
Correct |
457 ms |
35064 KB |
Output is correct |
10 |
Correct |
323 ms |
34680 KB |
Output is correct |
11 |
Correct |
421 ms |
34680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16256 KB |
Output is correct |
2 |
Correct |
2540 ms |
131716 KB |
Output is correct |
3 |
Correct |
3821 ms |
153080 KB |
Output is correct |
4 |
Correct |
841 ms |
150748 KB |
Output is correct |
5 |
Correct |
4638 ms |
188024 KB |
Output is correct |
6 |
Correct |
3381 ms |
155000 KB |
Output is correct |
7 |
Correct |
1062 ms |
61816 KB |
Output is correct |
8 |
Correct |
495 ms |
62316 KB |
Output is correct |
9 |
Correct |
1296 ms |
66808 KB |
Output is correct |
10 |
Correct |
1060 ms |
63228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
16384 KB |
Output is correct |
2 |
Correct |
395 ms |
25096 KB |
Output is correct |
3 |
Correct |
453 ms |
25080 KB |
Output is correct |
4 |
Correct |
426 ms |
25208 KB |
Output is correct |
5 |
Correct |
441 ms |
25464 KB |
Output is correct |
6 |
Correct |
313 ms |
34680 KB |
Output is correct |
7 |
Correct |
426 ms |
34680 KB |
Output is correct |
8 |
Correct |
445 ms |
34680 KB |
Output is correct |
9 |
Correct |
457 ms |
35064 KB |
Output is correct |
10 |
Correct |
323 ms |
34680 KB |
Output is correct |
11 |
Correct |
421 ms |
34680 KB |
Output is correct |
12 |
Correct |
14 ms |
16256 KB |
Output is correct |
13 |
Correct |
2540 ms |
131716 KB |
Output is correct |
14 |
Correct |
3821 ms |
153080 KB |
Output is correct |
15 |
Correct |
841 ms |
150748 KB |
Output is correct |
16 |
Correct |
4638 ms |
188024 KB |
Output is correct |
17 |
Correct |
3381 ms |
155000 KB |
Output is correct |
18 |
Correct |
1062 ms |
61816 KB |
Output is correct |
19 |
Correct |
495 ms |
62316 KB |
Output is correct |
20 |
Correct |
1296 ms |
66808 KB |
Output is correct |
21 |
Correct |
1060 ms |
63228 KB |
Output is correct |
22 |
Correct |
2906 ms |
157560 KB |
Output is correct |
23 |
Correct |
2809 ms |
160248 KB |
Output is correct |
24 |
Correct |
4372 ms |
160760 KB |
Output is correct |
25 |
Correct |
4080 ms |
164856 KB |
Output is correct |
26 |
Correct |
4046 ms |
161144 KB |
Output is correct |
27 |
Correct |
5914 ms |
189052 KB |
Output is correct |
28 |
Correct |
1052 ms |
161116 KB |
Output is correct |
29 |
Correct |
4485 ms |
160632 KB |
Output is correct |
30 |
Correct |
4545 ms |
159904 KB |
Output is correct |
31 |
Correct |
4666 ms |
160760 KB |
Output is correct |
32 |
Correct |
1241 ms |
67836 KB |
Output is correct |
33 |
Correct |
501 ms |
62696 KB |
Output is correct |
34 |
Correct |
811 ms |
59256 KB |
Output is correct |
35 |
Correct |
813 ms |
59256 KB |
Output is correct |
36 |
Correct |
1032 ms |
60024 KB |
Output is correct |
37 |
Correct |
1052 ms |
60024 KB |
Output is correct |