#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
typedef long long ll;
constexpr int LIM = 5e5;
vector<pair<int, int> >g[LIM + 10];
vector<pair<int, ll> >cen[LIM + 10];
ll cnt[LIM + 10];
int siz[LIM + 10];
bool vis[LIM + 10];
void cntsiz(int v, int o) {
siz[v] = 1;
for(auto x : g[v]) {
if(x.st != o) {
cntsiz(x.st, v);
siz[v] += siz[x.st];
}
}
}
void cntdist(int v, int o, int kt, ll d) {
cen[v].push_back({kt, d});
for(auto x : g[v]) {
if(x.st != o && !vis[x.st]) {
cntdist(x.st, v, kt, d + x.nd);
}
}
}
int find(int a) {
for(auto x : g[a]) {
if(!vis[x.st] && siz[x.st] > siz[a] / 2) {
int tmp = siz[x.st];
siz[x.st] = siz[a];
siz[a] -= tmp;
return find(x.st);
}
}
return a;
}
void dfs(int v) {
int c = find(v);
vis[c] = 1;
cntdist(c, c, c, 0);
for(auto x : g[c]) {
if(!vis[x.st]) {
dfs(x.st);
}
}
}
void Init(int n, int a[], int b[], int c[]) {
for(int i = 0; i < n - 1; i++) {
g[a[i]].push_back({b[i], c[i]});
g[b[i]].push_back({a[i], c[i]});
}
for(int i = 0; i < n; i++) {
cnt[i] = 1e18;
}
cntsiz(1, 1);
dfs(1);
}
ll Query(int n, int a[], int m, int b[]) {
vector<int>vs;
for(int i = 0; i < n; i++) {
for(auto x : cen[a[i]]) {
cnt[x.st] = min(cnt[x.st], x.nd);
vs.push_back(x.st);
}
}
ll ans = 1e18;
for(int i = 0; i < m; i++) {
for(auto x : cen[b[i]]) {
ans = min(ans, cnt[x.st] + x.nd);
}
}
for(auto x : vs) {
cnt[x] = 1e18;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24276 KB |
Output is correct |
2 |
Correct |
275 ms |
33444 KB |
Output is correct |
3 |
Correct |
353 ms |
33780 KB |
Output is correct |
4 |
Correct |
309 ms |
34172 KB |
Output is correct |
5 |
Correct |
319 ms |
34328 KB |
Output is correct |
6 |
Correct |
227 ms |
32828 KB |
Output is correct |
7 |
Correct |
295 ms |
33808 KB |
Output is correct |
8 |
Correct |
321 ms |
33952 KB |
Output is correct |
9 |
Correct |
338 ms |
34428 KB |
Output is correct |
10 |
Correct |
221 ms |
32832 KB |
Output is correct |
11 |
Correct |
287 ms |
33768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
24020 KB |
Output is correct |
2 |
Correct |
1948 ms |
189036 KB |
Output is correct |
3 |
Correct |
2459 ms |
258316 KB |
Output is correct |
4 |
Correct |
804 ms |
86196 KB |
Output is correct |
5 |
Correct |
3195 ms |
338304 KB |
Output is correct |
6 |
Correct |
2686 ms |
259232 KB |
Output is correct |
7 |
Correct |
919 ms |
67788 KB |
Output is correct |
8 |
Correct |
459 ms |
45600 KB |
Output is correct |
9 |
Correct |
1002 ms |
82056 KB |
Output is correct |
10 |
Correct |
913 ms |
68820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24276 KB |
Output is correct |
2 |
Correct |
275 ms |
33444 KB |
Output is correct |
3 |
Correct |
353 ms |
33780 KB |
Output is correct |
4 |
Correct |
309 ms |
34172 KB |
Output is correct |
5 |
Correct |
319 ms |
34328 KB |
Output is correct |
6 |
Correct |
227 ms |
32828 KB |
Output is correct |
7 |
Correct |
295 ms |
33808 KB |
Output is correct |
8 |
Correct |
321 ms |
33952 KB |
Output is correct |
9 |
Correct |
338 ms |
34428 KB |
Output is correct |
10 |
Correct |
221 ms |
32832 KB |
Output is correct |
11 |
Correct |
287 ms |
33768 KB |
Output is correct |
12 |
Correct |
15 ms |
24020 KB |
Output is correct |
13 |
Correct |
1948 ms |
189036 KB |
Output is correct |
14 |
Correct |
2459 ms |
258316 KB |
Output is correct |
15 |
Correct |
804 ms |
86196 KB |
Output is correct |
16 |
Correct |
3195 ms |
338304 KB |
Output is correct |
17 |
Correct |
2686 ms |
259232 KB |
Output is correct |
18 |
Correct |
919 ms |
67788 KB |
Output is correct |
19 |
Correct |
459 ms |
45600 KB |
Output is correct |
20 |
Correct |
1002 ms |
82056 KB |
Output is correct |
21 |
Correct |
913 ms |
68820 KB |
Output is correct |
22 |
Correct |
2121 ms |
192100 KB |
Output is correct |
23 |
Correct |
2186 ms |
198928 KB |
Output is correct |
24 |
Correct |
2948 ms |
268148 KB |
Output is correct |
25 |
Correct |
3015 ms |
267864 KB |
Output is correct |
26 |
Correct |
2913 ms |
261792 KB |
Output is correct |
27 |
Correct |
3689 ms |
375648 KB |
Output is correct |
28 |
Correct |
1066 ms |
115572 KB |
Output is correct |
29 |
Correct |
2967 ms |
284032 KB |
Output is correct |
30 |
Correct |
3070 ms |
285360 KB |
Output is correct |
31 |
Correct |
2873 ms |
285496 KB |
Output is correct |
32 |
Correct |
1058 ms |
101588 KB |
Output is correct |
33 |
Correct |
460 ms |
60028 KB |
Output is correct |
34 |
Correct |
726 ms |
73892 KB |
Output is correct |
35 |
Correct |
761 ms |
74788 KB |
Output is correct |
36 |
Correct |
883 ms |
79808 KB |
Output is correct |
37 |
Correct |
910 ms |
79884 KB |
Output is correct |