#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
char lvl[500001];
int par[500001], sz[500001];
ll dist[500001][20], ans[500001];
bool done[500001];
vector<pii> adj[500001];
void dfs(int u, int p, int curlvl, ll d) {
sz[u] = 1;
dist[u][curlvl] = d;
for (pii v : adj[u]) {
if (v.first != p && !done[v.first]) {
dfs(v.first, u, curlvl, d + v.second);
sz[u] += sz[v.first];
}
}
}
int centre(int u, int p, int tot) {
for (pii v : adj[u]) {
if (v.first != p && !done[v.first] && sz[v.first]<<1 > tot) {
return centre(v.first, u, tot);
}
}
return u;
}
void decomp(int i, int p, int curlvl, int tot) {
dfs(i, -1, curlvl, 0);
int c = centre(i, -1, tot);
dfs(c, -1, curlvl, 0);
done[c] = true;
lvl[c] = curlvl;
par[c] = p;
for (pii v : adj[c]) {
if (v.first != p && !done[v.first]) {
decomp(v.first, c, curlvl+1, sz[v.first]);
}
}
}
void Init(int N, int A[], int B[], int D[]) {
for (int i = 0; i < N-1; i++) {
adj[A[i]].push_back({B[i], D[i]});
adj[B[i]].push_back({A[i], D[i]});
}
for (int i = 0; i < 500000; i++)
ans[i] = 0x3f3f3f3f3f3f3f3f;
decomp(0, -1, 0, N);
}
long long Query(int S, int X[], int T, int Y[]) {
ll mn = 0x3f3f3f3f3f3f3f3f;
for (int i = 0; i < S; i++) {
int cur = X[i];
while(cur != -1) {
ans[cur] = min(ans[cur], dist[X[i]][lvl[cur]]);
cur = par[cur];
}
}
for (int i = 0; i < T; i++) {
int cur = Y[i];
while(cur != -1) {
mn = min(mn, dist[Y[i]][lvl[cur]] + ans[cur]);
cur = par[cur];
}
}
for (int i = 0; i < S; i++) {
int cur = X[i];
while(cur != -1) {
ans[cur] = 0x3f3f3f3f3f3f3f3f;
cur = par[cur];
}
}
return mn;
}
Compilation message
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:65:57: warning: array subscript has type 'char' [-Wchar-subscripts]
ans[cur] = min(ans[cur], dist[X[i]][lvl[cur]]);
^
factories.cpp:73:45: warning: array subscript has type 'char' [-Wchar-subscripts]
mn = min(mn, dist[Y[i]][lvl[cur]] + ans[cur]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
16632 KB |
Output is correct |
2 |
Correct |
304 ms |
34480 KB |
Output is correct |
3 |
Correct |
327 ms |
34600 KB |
Output is correct |
4 |
Correct |
326 ms |
34580 KB |
Output is correct |
5 |
Correct |
342 ms |
34808 KB |
Output is correct |
6 |
Correct |
249 ms |
34704 KB |
Output is correct |
7 |
Correct |
329 ms |
34808 KB |
Output is correct |
8 |
Correct |
334 ms |
34600 KB |
Output is correct |
9 |
Correct |
346 ms |
34960 KB |
Output is correct |
10 |
Correct |
244 ms |
34624 KB |
Output is correct |
11 |
Correct |
318 ms |
34676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
16248 KB |
Output is correct |
2 |
Correct |
2345 ms |
149328 KB |
Output is correct |
3 |
Correct |
3620 ms |
151212 KB |
Output is correct |
4 |
Correct |
956 ms |
149712 KB |
Output is correct |
5 |
Correct |
4614 ms |
180308 KB |
Output is correct |
6 |
Correct |
3566 ms |
153080 KB |
Output is correct |
7 |
Correct |
911 ms |
61304 KB |
Output is correct |
8 |
Correct |
448 ms |
62108 KB |
Output is correct |
9 |
Correct |
1178 ms |
65764 KB |
Output is correct |
10 |
Correct |
872 ms |
62912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
16632 KB |
Output is correct |
2 |
Correct |
304 ms |
34480 KB |
Output is correct |
3 |
Correct |
327 ms |
34600 KB |
Output is correct |
4 |
Correct |
326 ms |
34580 KB |
Output is correct |
5 |
Correct |
342 ms |
34808 KB |
Output is correct |
6 |
Correct |
249 ms |
34704 KB |
Output is correct |
7 |
Correct |
329 ms |
34808 KB |
Output is correct |
8 |
Correct |
334 ms |
34600 KB |
Output is correct |
9 |
Correct |
346 ms |
34960 KB |
Output is correct |
10 |
Correct |
244 ms |
34624 KB |
Output is correct |
11 |
Correct |
318 ms |
34676 KB |
Output is correct |
12 |
Correct |
13 ms |
16248 KB |
Output is correct |
13 |
Correct |
2345 ms |
149328 KB |
Output is correct |
14 |
Correct |
3620 ms |
151212 KB |
Output is correct |
15 |
Correct |
956 ms |
149712 KB |
Output is correct |
16 |
Correct |
4614 ms |
180308 KB |
Output is correct |
17 |
Correct |
3566 ms |
153080 KB |
Output is correct |
18 |
Correct |
911 ms |
61304 KB |
Output is correct |
19 |
Correct |
448 ms |
62108 KB |
Output is correct |
20 |
Correct |
1178 ms |
65764 KB |
Output is correct |
21 |
Correct |
872 ms |
62912 KB |
Output is correct |
22 |
Correct |
2041 ms |
138368 KB |
Output is correct |
23 |
Correct |
2456 ms |
142444 KB |
Output is correct |
24 |
Correct |
4267 ms |
142584 KB |
Output is correct |
25 |
Correct |
4139 ms |
146292 KB |
Output is correct |
26 |
Correct |
3555 ms |
142952 KB |
Output is correct |
27 |
Correct |
4088 ms |
165968 KB |
Output is correct |
28 |
Correct |
997 ms |
143204 KB |
Output is correct |
29 |
Correct |
3228 ms |
142524 KB |
Output is correct |
30 |
Correct |
3352 ms |
141944 KB |
Output is correct |
31 |
Correct |
3624 ms |
142516 KB |
Output is correct |
32 |
Correct |
1344 ms |
66760 KB |
Output is correct |
33 |
Correct |
428 ms |
62444 KB |
Output is correct |
34 |
Correct |
702 ms |
59128 KB |
Output is correct |
35 |
Correct |
725 ms |
59064 KB |
Output is correct |
36 |
Correct |
1010 ms |
59988 KB |
Output is correct |
37 |
Correct |
954 ms |
59736 KB |
Output is correct |