#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int msz = 2e5 + 5;
const int lg = 25;
struct edge{
int to, t;
edge(int a, int b){
to = a; t = b;
}
};
vector<edge> g[msz];
vector<int> cl[lg], out, sz;
void fill(int v, int pr){
sz[v] = 1;
for (auto i: g[v]){
if (i.to == pr || out[i.to]) continue;
fill(i.to, v);
sz[v] += sz[i.to];
}
}
int find(int v, int pr, int& x){
for (auto i: g[v]){
if (i.to == pr || out[i.to]) continue;
if (2 * sz[i.to] >= x) return find(i.to, v, x);
}
return v;
}
void build(int v, int cnt){
fill(v, 0);
int u = find(v, 0, sz[v]);
out[u] = cnt++;
cl[out[u]].push_back(u);
for (auto i: g[u]){
if (out[i.to]) continue;
build(i.to, cnt);
}
}
vector<ll> weight;
vector<pair<int, int>> p;
vector<bool> used;
void fill_dfs(int v, int pr){
for (auto i: g[v]){
if (i.to == pr || used[i.to]) continue;
weight[i.to] = weight[v] + i.t;
fill_dfs(i.to, v);
}
}
void add(int v, int pr, int cnt){
p.push_back({v, cnt++});
for (auto i: g[v]){
if (i.to == pr || used[i.to]) continue;
add(i.to, v, cnt);
}
}
int ans = msz, l;
void solve(int v, int cnt){
weight[v] = 0;
used[v] = true;
fill_dfs(v, 0);
map<ll, int> mp;
for (auto i: g[v]){
if (used[i.to]) continue;
add(i.to, 0, 1);
for (auto j: p){
if (weight[j.first] > l) continue;
if (weight[j.first] == l){
ans = min(ans, j.second);
continue;
}
int x = mp[l - weight[j.first]];
if (x > 0) ans = min(ans, j.second + x);
}
for (auto j: p){
int val = mp[weight[j.first]];
if (!val){
mp[weight[j.first]] = j.second;
continue;
}
mp[weight[j.first]] = min(val, j.second);
}
p.clear();
}
}
int n;
ll best_path(int ns, int ks, int hs[][2], int ls[]){
n = ns; l = ks;
for (int i = 0; i < n; i++){
auto [u, v] = hs[i];
u++; v++;
g[u].push_back({v, ls[i]});
g[v].push_back({u, ls[i]});
}
out.assign(n + 1, 0);
sz.resize(n + 1);
build(1, 1);
weight.resize(n + 1);
used.assign(n + 1, false);
for (int i = 1; i < lg; i++){
for (int j: cl[i]){
solve(j, 1);
}
}
if (ans == msz){
return -1;
}
return ans;
}
// Old Code
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8784 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8784 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8804 KB |
Output is correct |
21 |
Correct |
3 ms |
8796 KB |
Output is correct |
22 |
Correct |
4 ms |
8876 KB |
Output is correct |
23 |
Correct |
3 ms |
8792 KB |
Output is correct |
24 |
Correct |
3 ms |
8796 KB |
Output is correct |
25 |
Correct |
3 ms |
8792 KB |
Output is correct |
26 |
Correct |
3 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
3 ms |
8796 KB |
Output is correct |
29 |
Correct |
4 ms |
8800 KB |
Output is correct |
30 |
Correct |
3 ms |
8796 KB |
Output is correct |
31 |
Correct |
3 ms |
8796 KB |
Output is correct |
32 |
Correct |
4 ms |
8796 KB |
Output is correct |
33 |
Correct |
4 ms |
8792 KB |
Output is correct |
34 |
Correct |
3 ms |
8796 KB |
Output is correct |
35 |
Correct |
3 ms |
8796 KB |
Output is correct |
36 |
Correct |
4 ms |
8796 KB |
Output is correct |
37 |
Correct |
3 ms |
9048 KB |
Output is correct |
38 |
Correct |
4 ms |
8892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8784 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
262 ms |
18080 KB |
Output is correct |
20 |
Correct |
249 ms |
18956 KB |
Output is correct |
21 |
Correct |
218 ms |
18556 KB |
Output is correct |
22 |
Correct |
192 ms |
19168 KB |
Output is correct |
23 |
Correct |
223 ms |
19532 KB |
Output is correct |
24 |
Correct |
122 ms |
19104 KB |
Output is correct |
25 |
Correct |
185 ms |
22020 KB |
Output is correct |
26 |
Correct |
106 ms |
25556 KB |
Output is correct |
27 |
Correct |
259 ms |
24352 KB |
Output is correct |
28 |
Correct |
795 ms |
52744 KB |
Output is correct |
29 |
Correct |
811 ms |
51564 KB |
Output is correct |
30 |
Correct |
310 ms |
24892 KB |
Output is correct |
31 |
Correct |
253 ms |
24968 KB |
Output is correct |
32 |
Correct |
386 ms |
26948 KB |
Output is correct |
33 |
Correct |
414 ms |
26052 KB |
Output is correct |
34 |
Correct |
758 ms |
37948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8784 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8804 KB |
Output is correct |
21 |
Correct |
3 ms |
8796 KB |
Output is correct |
22 |
Correct |
4 ms |
8876 KB |
Output is correct |
23 |
Correct |
3 ms |
8792 KB |
Output is correct |
24 |
Correct |
3 ms |
8796 KB |
Output is correct |
25 |
Correct |
3 ms |
8792 KB |
Output is correct |
26 |
Correct |
3 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
3 ms |
8796 KB |
Output is correct |
29 |
Correct |
4 ms |
8800 KB |
Output is correct |
30 |
Correct |
3 ms |
8796 KB |
Output is correct |
31 |
Correct |
3 ms |
8796 KB |
Output is correct |
32 |
Correct |
4 ms |
8796 KB |
Output is correct |
33 |
Correct |
4 ms |
8792 KB |
Output is correct |
34 |
Correct |
3 ms |
8796 KB |
Output is correct |
35 |
Correct |
3 ms |
8796 KB |
Output is correct |
36 |
Correct |
4 ms |
8796 KB |
Output is correct |
37 |
Correct |
3 ms |
9048 KB |
Output is correct |
38 |
Correct |
4 ms |
8892 KB |
Output is correct |
39 |
Correct |
262 ms |
18080 KB |
Output is correct |
40 |
Correct |
249 ms |
18956 KB |
Output is correct |
41 |
Correct |
218 ms |
18556 KB |
Output is correct |
42 |
Correct |
192 ms |
19168 KB |
Output is correct |
43 |
Correct |
223 ms |
19532 KB |
Output is correct |
44 |
Correct |
122 ms |
19104 KB |
Output is correct |
45 |
Correct |
185 ms |
22020 KB |
Output is correct |
46 |
Correct |
106 ms |
25556 KB |
Output is correct |
47 |
Correct |
259 ms |
24352 KB |
Output is correct |
48 |
Correct |
795 ms |
52744 KB |
Output is correct |
49 |
Correct |
811 ms |
51564 KB |
Output is correct |
50 |
Correct |
310 ms |
24892 KB |
Output is correct |
51 |
Correct |
253 ms |
24968 KB |
Output is correct |
52 |
Correct |
386 ms |
26948 KB |
Output is correct |
53 |
Correct |
414 ms |
26052 KB |
Output is correct |
54 |
Correct |
758 ms |
37948 KB |
Output is correct |
55 |
Correct |
29 ms |
9820 KB |
Output is correct |
56 |
Correct |
16 ms |
9564 KB |
Output is correct |
57 |
Correct |
143 ms |
18448 KB |
Output is correct |
58 |
Correct |
39 ms |
18384 KB |
Output is correct |
59 |
Correct |
343 ms |
34692 KB |
Output is correct |
60 |
Correct |
1128 ms |
60884 KB |
Output is correct |
61 |
Correct |
310 ms |
25696 KB |
Output is correct |
62 |
Correct |
288 ms |
24908 KB |
Output is correct |
63 |
Correct |
610 ms |
26772 KB |
Output is correct |
64 |
Correct |
1378 ms |
37240 KB |
Output is correct |
65 |
Correct |
905 ms |
39352 KB |
Output is correct |
66 |
Correct |
972 ms |
46980 KB |
Output is correct |
67 |
Correct |
176 ms |
30140 KB |
Output is correct |
68 |
Correct |
718 ms |
41780 KB |
Output is correct |
69 |
Correct |
656 ms |
39908 KB |
Output is correct |
70 |
Correct |
616 ms |
40984 KB |
Output is correct |