#include <bits/stdc++.h>
#include "factories.h"
#define pb push_back
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define s second
using namespace std;
typedef pair<ll, ll> pii;
const int MAXN = 5e5+5;
ll sz[MAXN], used[MAXN], best[MAXN];
vector<pii> g[MAXN], ng[MAXN];
int getsz(int u, int p) {
sz[u] = 1;
for(pii &i : g[u])
if(i.f != p && !used[i.f])
sz[u] += getsz(i.f, u);
return sz[u];
}
int findc(int u, int p, int n) {
for(pii &i : g[u])
if(i.f != p && !used[i.f] && sz[i.f] > n/2)
return findc(i.f, u, n);
return u;
}
void getng(int u, int p, int c, ll dist) {
ng[u].pb({c, dist});
for(pii &i : g[u])
if(i.f != p && !used[i.f])
getng(i.f, u, c, dist + i.s);
}
void build(int u) {
int c = findc(u, -1, getsz(u, -1));
getng(c, -1, c, 0);
used[c] = 1;
for(pii &i : g[c])
if(!used[i.f])
build(i.f);
}
void Init(int n, int a[], int b[], int c[]) {
for(int i = 0; i < n-1; i++) {
g[a[i]].pb({b[i], c[i]});
g[b[i]].pb({a[i], c[i]});
}
build(0);
fill(best, best + MAXN, LINF);
}
ll Query(int s, int x[], int t, int y[]) {
ll ans = LINF;
for(int u = 0; u < s; u++)
for(pii &i : ng[x[u]])
best[i.f] = min(best[i.f], i.s);
for(int u = 0; u < t; u++)
for(pii &i : ng[y[u]])
ans = min(ans, best[i.f] + i.s);
for(int u = 0; u < s; u++)
for(pii &i : ng[x[u]])
best[i.f] = LINF;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
28236 KB |
Output is correct |
2 |
Correct |
354 ms |
39316 KB |
Output is correct |
3 |
Correct |
370 ms |
37824 KB |
Output is correct |
4 |
Correct |
383 ms |
37864 KB |
Output is correct |
5 |
Correct |
404 ms |
38260 KB |
Output is correct |
6 |
Correct |
278 ms |
36884 KB |
Output is correct |
7 |
Correct |
370 ms |
37784 KB |
Output is correct |
8 |
Correct |
379 ms |
37908 KB |
Output is correct |
9 |
Correct |
407 ms |
38316 KB |
Output is correct |
10 |
Correct |
304 ms |
36900 KB |
Output is correct |
11 |
Correct |
368 ms |
37764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
27888 KB |
Output is correct |
2 |
Correct |
2666 ms |
201208 KB |
Output is correct |
3 |
Correct |
3854 ms |
271740 KB |
Output is correct |
4 |
Correct |
994 ms |
95444 KB |
Output is correct |
5 |
Correct |
4704 ms |
346652 KB |
Output is correct |
6 |
Correct |
3951 ms |
272684 KB |
Output is correct |
7 |
Correct |
1174 ms |
76840 KB |
Output is correct |
8 |
Correct |
514 ms |
53808 KB |
Output is correct |
9 |
Correct |
1277 ms |
90748 KB |
Output is correct |
10 |
Correct |
1187 ms |
78064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
28236 KB |
Output is correct |
2 |
Correct |
354 ms |
39316 KB |
Output is correct |
3 |
Correct |
370 ms |
37824 KB |
Output is correct |
4 |
Correct |
383 ms |
37864 KB |
Output is correct |
5 |
Correct |
404 ms |
38260 KB |
Output is correct |
6 |
Correct |
278 ms |
36884 KB |
Output is correct |
7 |
Correct |
370 ms |
37784 KB |
Output is correct |
8 |
Correct |
379 ms |
37908 KB |
Output is correct |
9 |
Correct |
407 ms |
38316 KB |
Output is correct |
10 |
Correct |
304 ms |
36900 KB |
Output is correct |
11 |
Correct |
368 ms |
37764 KB |
Output is correct |
12 |
Correct |
19 ms |
27888 KB |
Output is correct |
13 |
Correct |
2666 ms |
201208 KB |
Output is correct |
14 |
Correct |
3854 ms |
271740 KB |
Output is correct |
15 |
Correct |
994 ms |
95444 KB |
Output is correct |
16 |
Correct |
4704 ms |
346652 KB |
Output is correct |
17 |
Correct |
3951 ms |
272684 KB |
Output is correct |
18 |
Correct |
1174 ms |
76840 KB |
Output is correct |
19 |
Correct |
514 ms |
53808 KB |
Output is correct |
20 |
Correct |
1277 ms |
90748 KB |
Output is correct |
21 |
Correct |
1187 ms |
78064 KB |
Output is correct |
22 |
Correct |
3126 ms |
200924 KB |
Output is correct |
23 |
Correct |
3177 ms |
205332 KB |
Output is correct |
24 |
Correct |
4592 ms |
273384 KB |
Output is correct |
25 |
Correct |
4522 ms |
277196 KB |
Output is correct |
26 |
Correct |
4386 ms |
274568 KB |
Output is correct |
27 |
Correct |
5529 ms |
347052 KB |
Output is correct |
28 |
Correct |
1382 ms |
99220 KB |
Output is correct |
29 |
Correct |
4681 ms |
273416 KB |
Output is correct |
30 |
Correct |
4617 ms |
273568 KB |
Output is correct |
31 |
Correct |
4694 ms |
273452 KB |
Output is correct |
32 |
Correct |
1499 ms |
89348 KB |
Output is correct |
33 |
Correct |
606 ms |
52336 KB |
Output is correct |
34 |
Correct |
977 ms |
67748 KB |
Output is correct |
35 |
Correct |
1047 ms |
68412 KB |
Output is correct |
36 |
Correct |
1211 ms |
73436 KB |
Output is correct |
37 |
Correct |
1245 ms |
73532 KB |
Output is correct |