#include <bits/stdc++.h>
#include "race.h"
using namespace std;
// #define int long long
#define ll long long
#define pii pair<int, int>
#define F first
#define S second
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define FOR(i, n) for (int i = 0; i < n; i++)
#define NYOOM ios::sync_with_stdio(0); cin.tie(0);
#define endl '\n'
const int INF = 1e9 + 7;
const ll LLINF = 1ll<<60;
const int maxn = 2e5 + 10;
int n, k, sz[maxn];
vector<pair<pii, int>> adj[maxn];
map<int, int> small, large;
set<int> deleted;
void init(int v, int p){
sz[v] = 1;
for (auto x : adj[v]){
int u = x.F.F, w = x.F.S, i = x.S;
if (deleted.count(i)) continue;
if (u == p) continue;
init(u, v); sz[v] += sz[u];
}
}
int get_centroid(int v, int p, int pog){
for (auto x : adj[v]){
int u = x.F.F, w = x.F.S, i = x.S;
if (deleted.count(i)) continue;
if (u == p) continue;
if (sz[u] * 2 > pog) return get_centroid(u, v, pog);
}
return v;
}
void dfs(int v, int p, int h, int cnt){
if (small.count(h)) small[h] = min(small[h], cnt);
else small[h] = cnt;
for (auto x : adj[v]){
int u = x.F.F, w = x.F.S, i = x.S;
if (deleted.count(i)) continue;
if (u == p) continue;
dfs(u, v, h + w, cnt + 1);
}
}
int solve(int v){
init(v, v);
v = get_centroid(v, -1, sz[v]);
int re = INF;
large.clear();
small.clear();
large[0] = 0;
for (auto x : adj[v]){
int u = x.F.F, w = x.F.S, i = x.S;
if (deleted.count(i)) continue;
dfs(u, v, w, 1);
for (auto bruh : small){
if (large.count(k - bruh.F)){
re = min(re, bruh.S + large[k - bruh.F]);
}
}
for (auto bruh : small){
if (large.count(bruh.F)) large[bruh.F] = min(large[bruh.F], bruh.S);
else large[bruh.F] = bruh.S;
}
small.clear();
}
for (auto x : adj[v]){
int u = x.F.F, w = x.F.S, i = x.S;
if (deleted.count(i)) continue;
deleted.insert(i);
re = min(re, solve(u));
}
return re;
}
int best_path(int N, int K, int h[][2], int l[]){
n = N, k = K;
FOR(i, n - 1){
adj[h[i][0]].PB({{h[i][1], l[i]}, i});
adj[h[i][1]].PB({{h[i][0], l[i]}, i});
}
int re = solve(0);
return (re == INF ? -1 : re);
}
Compilation message
race.cpp: In function 'void init(int, int)':
race.cpp:27:24: warning: unused variable 'w' [-Wunused-variable]
27 | int u = x.F.F, w = x.F.S, i = x.S;
| ^
race.cpp: In function 'int get_centroid(int, int, int)':
race.cpp:36:24: warning: unused variable 'w' [-Wunused-variable]
36 | int u = x.F.F, w = x.F.S, i = x.S;
| ^
race.cpp: In function 'int solve(int)':
race.cpp:78:24: warning: unused variable 'w' [-Wunused-variable]
78 | int u = x.F.F, w = x.F.S, i = x.S;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5024 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
5 ms |
5076 KB |
Output is correct |
22 |
Correct |
6 ms |
5132 KB |
Output is correct |
23 |
Correct |
6 ms |
5076 KB |
Output is correct |
24 |
Correct |
6 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5076 KB |
Output is correct |
26 |
Correct |
6 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
5 ms |
5076 KB |
Output is correct |
29 |
Correct |
5 ms |
5076 KB |
Output is correct |
30 |
Correct |
5 ms |
5076 KB |
Output is correct |
31 |
Correct |
5 ms |
5076 KB |
Output is correct |
32 |
Correct |
6 ms |
5076 KB |
Output is correct |
33 |
Correct |
6 ms |
5076 KB |
Output is correct |
34 |
Correct |
5 ms |
5068 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
5 ms |
5076 KB |
Output is correct |
37 |
Correct |
5 ms |
5076 KB |
Output is correct |
38 |
Correct |
5 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
750 ms |
15460 KB |
Output is correct |
20 |
Correct |
787 ms |
15308 KB |
Output is correct |
21 |
Correct |
718 ms |
15392 KB |
Output is correct |
22 |
Correct |
623 ms |
15572 KB |
Output is correct |
23 |
Correct |
579 ms |
15960 KB |
Output is correct |
24 |
Correct |
271 ms |
15628 KB |
Output is correct |
25 |
Correct |
421 ms |
19028 KB |
Output is correct |
26 |
Correct |
347 ms |
20592 KB |
Output is correct |
27 |
Correct |
1070 ms |
25688 KB |
Output is correct |
28 |
Correct |
1638 ms |
40844 KB |
Output is correct |
29 |
Correct |
1630 ms |
40072 KB |
Output is correct |
30 |
Correct |
1076 ms |
25992 KB |
Output is correct |
31 |
Correct |
1147 ms |
25804 KB |
Output is correct |
32 |
Correct |
1168 ms |
25780 KB |
Output is correct |
33 |
Correct |
959 ms |
23944 KB |
Output is correct |
34 |
Correct |
1401 ms |
26388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5024 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
5 ms |
5076 KB |
Output is correct |
22 |
Correct |
6 ms |
5132 KB |
Output is correct |
23 |
Correct |
6 ms |
5076 KB |
Output is correct |
24 |
Correct |
6 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5076 KB |
Output is correct |
26 |
Correct |
6 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
5 ms |
5076 KB |
Output is correct |
29 |
Correct |
5 ms |
5076 KB |
Output is correct |
30 |
Correct |
5 ms |
5076 KB |
Output is correct |
31 |
Correct |
5 ms |
5076 KB |
Output is correct |
32 |
Correct |
6 ms |
5076 KB |
Output is correct |
33 |
Correct |
6 ms |
5076 KB |
Output is correct |
34 |
Correct |
5 ms |
5068 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
5 ms |
5076 KB |
Output is correct |
37 |
Correct |
5 ms |
5076 KB |
Output is correct |
38 |
Correct |
5 ms |
5076 KB |
Output is correct |
39 |
Correct |
750 ms |
15460 KB |
Output is correct |
40 |
Correct |
787 ms |
15308 KB |
Output is correct |
41 |
Correct |
718 ms |
15392 KB |
Output is correct |
42 |
Correct |
623 ms |
15572 KB |
Output is correct |
43 |
Correct |
579 ms |
15960 KB |
Output is correct |
44 |
Correct |
271 ms |
15628 KB |
Output is correct |
45 |
Correct |
421 ms |
19028 KB |
Output is correct |
46 |
Correct |
347 ms |
20592 KB |
Output is correct |
47 |
Correct |
1070 ms |
25688 KB |
Output is correct |
48 |
Correct |
1638 ms |
40844 KB |
Output is correct |
49 |
Correct |
1630 ms |
40072 KB |
Output is correct |
50 |
Correct |
1076 ms |
25992 KB |
Output is correct |
51 |
Correct |
1147 ms |
25804 KB |
Output is correct |
52 |
Correct |
1168 ms |
25780 KB |
Output is correct |
53 |
Correct |
959 ms |
23944 KB |
Output is correct |
54 |
Correct |
1401 ms |
26388 KB |
Output is correct |
55 |
Correct |
51 ms |
6088 KB |
Output is correct |
56 |
Correct |
41 ms |
6080 KB |
Output is correct |
57 |
Correct |
364 ms |
15964 KB |
Output is correct |
58 |
Correct |
110 ms |
15616 KB |
Output is correct |
59 |
Correct |
686 ms |
21940 KB |
Output is correct |
60 |
Correct |
1675 ms |
39756 KB |
Output is correct |
61 |
Correct |
1171 ms |
25944 KB |
Output is correct |
62 |
Correct |
1078 ms |
25704 KB |
Output is correct |
63 |
Correct |
1167 ms |
25732 KB |
Output is correct |
64 |
Correct |
2480 ms |
25992 KB |
Output is correct |
65 |
Correct |
2091 ms |
26480 KB |
Output is correct |
66 |
Correct |
1660 ms |
38096 KB |
Output is correct |
67 |
Correct |
265 ms |
26124 KB |
Output is correct |
68 |
Correct |
1277 ms |
25940 KB |
Output is correct |
69 |
Correct |
1240 ms |
26096 KB |
Output is correct |
70 |
Correct |
1152 ms |
25088 KB |
Output is correct |