/*
ghmt the cutie :3
UwU
*/
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define INF 1e9
#define f first
#define s second
#define pii pair<int, int>
#define vi vector<int>
const int MAXN = 2e5;
int n, k;
vector<pii> g[MAXN + 5];
int h[MAXN + 5], d[MAXN + 5];
int res = INF;
map<int, int> mp[MAXN + 5];
void pre_dfs(int u, int par = -1) {
mp[u][d[u]] = h[u];
for(auto [v, w] : g[u]) {
if(v == par) continue;
h[v] = h[u] + 1;
d[v] = d[u] + w;
pre_dfs(v, u);
}
}
// mp[u][w1]
void dfs(int u, int par = -1) {
for(auto [v, w] : g[u]) {
if(v == par) continue;
dfs(v, u);
if(mp[u].size() < mp[v].size()) swap(mp[u], mp[v]);
for(auto it : mp[v]) {
int need = (k + 2 * d[u]) - it.f;
if(mp[u].find(need) != mp[u].end()) {
// cout << u << ' ' << v << ' ' << it.f << ' ' << k << ' ' << it.s + mp[u][need] - 2 * d[u] << '\n';
res = min(res, it.s + mp[u][need] - 2 * h[u]);
}
}
for(auto it : mp[v]) {
if(mp[u].find(it.f) != mp[u].end()) mp[u][it.f] = min(mp[u][it.f], it.s);
else mp[u][it.f] = it.s;
}
}
}
int solve() {
memset(h, 0, sizeof(h));
memset(d, 0, sizeof(d));
pre_dfs(1, 1);
// for(int i = 1; i <= n; i++) cout << i << ' ' << h[i] << '\n'; cout << '\n';
dfs(1, 1);
if(res == INF) return -1;
else return res;
}
int best_path(int N, int K, int H[][2], int L[])
{
// cout << "saygex\n";
n = N, k = K;
for(int i = 0; i < n - 1; i++) {
int u = H[i][0], v = H[i][1], w = L[i];
u++, v++;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
return solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19536 KB |
Output is correct |
2 |
Correct |
3 ms |
19536 KB |
Output is correct |
3 |
Correct |
4 ms |
19536 KB |
Output is correct |
4 |
Correct |
3 ms |
19536 KB |
Output is correct |
5 |
Correct |
3 ms |
19536 KB |
Output is correct |
6 |
Correct |
4 ms |
19536 KB |
Output is correct |
7 |
Correct |
3 ms |
19536 KB |
Output is correct |
8 |
Correct |
4 ms |
19536 KB |
Output is correct |
9 |
Correct |
3 ms |
19536 KB |
Output is correct |
10 |
Correct |
4 ms |
19536 KB |
Output is correct |
11 |
Correct |
3 ms |
19536 KB |
Output is correct |
12 |
Correct |
4 ms |
19536 KB |
Output is correct |
13 |
Correct |
4 ms |
19536 KB |
Output is correct |
14 |
Correct |
3 ms |
19536 KB |
Output is correct |
15 |
Correct |
3 ms |
19536 KB |
Output is correct |
16 |
Correct |
4 ms |
19536 KB |
Output is correct |
17 |
Correct |
4 ms |
19536 KB |
Output is correct |
18 |
Correct |
3 ms |
19536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19536 KB |
Output is correct |
2 |
Correct |
3 ms |
19536 KB |
Output is correct |
3 |
Correct |
4 ms |
19536 KB |
Output is correct |
4 |
Correct |
3 ms |
19536 KB |
Output is correct |
5 |
Correct |
3 ms |
19536 KB |
Output is correct |
6 |
Correct |
4 ms |
19536 KB |
Output is correct |
7 |
Correct |
3 ms |
19536 KB |
Output is correct |
8 |
Correct |
4 ms |
19536 KB |
Output is correct |
9 |
Correct |
3 ms |
19536 KB |
Output is correct |
10 |
Correct |
4 ms |
19536 KB |
Output is correct |
11 |
Correct |
3 ms |
19536 KB |
Output is correct |
12 |
Correct |
4 ms |
19536 KB |
Output is correct |
13 |
Correct |
4 ms |
19536 KB |
Output is correct |
14 |
Correct |
3 ms |
19536 KB |
Output is correct |
15 |
Correct |
3 ms |
19536 KB |
Output is correct |
16 |
Correct |
4 ms |
19536 KB |
Output is correct |
17 |
Correct |
4 ms |
19536 KB |
Output is correct |
18 |
Correct |
3 ms |
19536 KB |
Output is correct |
19 |
Correct |
4 ms |
19708 KB |
Output is correct |
20 |
Correct |
3 ms |
19536 KB |
Output is correct |
21 |
Correct |
4 ms |
19792 KB |
Output is correct |
22 |
Correct |
4 ms |
19792 KB |
Output is correct |
23 |
Correct |
4 ms |
19792 KB |
Output is correct |
24 |
Correct |
4 ms |
19752 KB |
Output is correct |
25 |
Correct |
5 ms |
19792 KB |
Output is correct |
26 |
Correct |
5 ms |
19792 KB |
Output is correct |
27 |
Correct |
4 ms |
19720 KB |
Output is correct |
28 |
Correct |
4 ms |
19792 KB |
Output is correct |
29 |
Correct |
4 ms |
19792 KB |
Output is correct |
30 |
Correct |
5 ms |
19756 KB |
Output is correct |
31 |
Correct |
4 ms |
19792 KB |
Output is correct |
32 |
Correct |
4 ms |
19896 KB |
Output is correct |
33 |
Correct |
4 ms |
19792 KB |
Output is correct |
34 |
Correct |
4 ms |
19792 KB |
Output is correct |
35 |
Correct |
4 ms |
19792 KB |
Output is correct |
36 |
Correct |
5 ms |
19792 KB |
Output is correct |
37 |
Correct |
5 ms |
19792 KB |
Output is correct |
38 |
Correct |
5 ms |
19804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19536 KB |
Output is correct |
2 |
Correct |
3 ms |
19536 KB |
Output is correct |
3 |
Correct |
4 ms |
19536 KB |
Output is correct |
4 |
Correct |
3 ms |
19536 KB |
Output is correct |
5 |
Correct |
3 ms |
19536 KB |
Output is correct |
6 |
Correct |
4 ms |
19536 KB |
Output is correct |
7 |
Correct |
3 ms |
19536 KB |
Output is correct |
8 |
Correct |
4 ms |
19536 KB |
Output is correct |
9 |
Correct |
3 ms |
19536 KB |
Output is correct |
10 |
Correct |
4 ms |
19536 KB |
Output is correct |
11 |
Correct |
3 ms |
19536 KB |
Output is correct |
12 |
Correct |
4 ms |
19536 KB |
Output is correct |
13 |
Correct |
4 ms |
19536 KB |
Output is correct |
14 |
Correct |
3 ms |
19536 KB |
Output is correct |
15 |
Correct |
3 ms |
19536 KB |
Output is correct |
16 |
Correct |
4 ms |
19536 KB |
Output is correct |
17 |
Correct |
4 ms |
19536 KB |
Output is correct |
18 |
Correct |
3 ms |
19536 KB |
Output is correct |
19 |
Correct |
83 ms |
38576 KB |
Output is correct |
20 |
Correct |
91 ms |
38664 KB |
Output is correct |
21 |
Correct |
86 ms |
38472 KB |
Output is correct |
22 |
Correct |
101 ms |
38068 KB |
Output is correct |
23 |
Correct |
131 ms |
47944 KB |
Output is correct |
24 |
Correct |
90 ms |
40776 KB |
Output is correct |
25 |
Correct |
89 ms |
39656 KB |
Output is correct |
26 |
Correct |
72 ms |
46668 KB |
Output is correct |
27 |
Correct |
167 ms |
46664 KB |
Output is correct |
28 |
Correct |
216 ms |
81224 KB |
Output is correct |
29 |
Correct |
229 ms |
79668 KB |
Output is correct |
30 |
Correct |
136 ms |
46516 KB |
Output is correct |
31 |
Correct |
135 ms |
46768 KB |
Output is correct |
32 |
Correct |
173 ms |
46928 KB |
Output is correct |
33 |
Correct |
176 ms |
50216 KB |
Output is correct |
34 |
Correct |
252 ms |
75080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19536 KB |
Output is correct |
2 |
Correct |
3 ms |
19536 KB |
Output is correct |
3 |
Correct |
4 ms |
19536 KB |
Output is correct |
4 |
Correct |
3 ms |
19536 KB |
Output is correct |
5 |
Correct |
3 ms |
19536 KB |
Output is correct |
6 |
Correct |
4 ms |
19536 KB |
Output is correct |
7 |
Correct |
3 ms |
19536 KB |
Output is correct |
8 |
Correct |
4 ms |
19536 KB |
Output is correct |
9 |
Correct |
3 ms |
19536 KB |
Output is correct |
10 |
Correct |
4 ms |
19536 KB |
Output is correct |
11 |
Correct |
3 ms |
19536 KB |
Output is correct |
12 |
Correct |
4 ms |
19536 KB |
Output is correct |
13 |
Correct |
4 ms |
19536 KB |
Output is correct |
14 |
Correct |
3 ms |
19536 KB |
Output is correct |
15 |
Correct |
3 ms |
19536 KB |
Output is correct |
16 |
Correct |
4 ms |
19536 KB |
Output is correct |
17 |
Correct |
4 ms |
19536 KB |
Output is correct |
18 |
Correct |
3 ms |
19536 KB |
Output is correct |
19 |
Correct |
4 ms |
19708 KB |
Output is correct |
20 |
Correct |
3 ms |
19536 KB |
Output is correct |
21 |
Correct |
4 ms |
19792 KB |
Output is correct |
22 |
Correct |
4 ms |
19792 KB |
Output is correct |
23 |
Correct |
4 ms |
19792 KB |
Output is correct |
24 |
Correct |
4 ms |
19752 KB |
Output is correct |
25 |
Correct |
5 ms |
19792 KB |
Output is correct |
26 |
Correct |
5 ms |
19792 KB |
Output is correct |
27 |
Correct |
4 ms |
19720 KB |
Output is correct |
28 |
Correct |
4 ms |
19792 KB |
Output is correct |
29 |
Correct |
4 ms |
19792 KB |
Output is correct |
30 |
Correct |
5 ms |
19756 KB |
Output is correct |
31 |
Correct |
4 ms |
19792 KB |
Output is correct |
32 |
Correct |
4 ms |
19896 KB |
Output is correct |
33 |
Correct |
4 ms |
19792 KB |
Output is correct |
34 |
Correct |
4 ms |
19792 KB |
Output is correct |
35 |
Correct |
4 ms |
19792 KB |
Output is correct |
36 |
Correct |
5 ms |
19792 KB |
Output is correct |
37 |
Correct |
5 ms |
19792 KB |
Output is correct |
38 |
Correct |
5 ms |
19804 KB |
Output is correct |
39 |
Correct |
83 ms |
38576 KB |
Output is correct |
40 |
Correct |
91 ms |
38664 KB |
Output is correct |
41 |
Correct |
86 ms |
38472 KB |
Output is correct |
42 |
Correct |
101 ms |
38068 KB |
Output is correct |
43 |
Correct |
131 ms |
47944 KB |
Output is correct |
44 |
Correct |
90 ms |
40776 KB |
Output is correct |
45 |
Correct |
89 ms |
39656 KB |
Output is correct |
46 |
Correct |
72 ms |
46668 KB |
Output is correct |
47 |
Correct |
167 ms |
46664 KB |
Output is correct |
48 |
Correct |
216 ms |
81224 KB |
Output is correct |
49 |
Correct |
229 ms |
79668 KB |
Output is correct |
50 |
Correct |
136 ms |
46516 KB |
Output is correct |
51 |
Correct |
135 ms |
46768 KB |
Output is correct |
52 |
Correct |
173 ms |
46928 KB |
Output is correct |
53 |
Correct |
176 ms |
50216 KB |
Output is correct |
54 |
Correct |
252 ms |
75080 KB |
Output is correct |
55 |
Correct |
14 ms |
22096 KB |
Output is correct |
56 |
Correct |
8 ms |
21072 KB |
Output is correct |
57 |
Correct |
55 ms |
36552 KB |
Output is correct |
58 |
Correct |
37 ms |
31420 KB |
Output is correct |
59 |
Correct |
72 ms |
51272 KB |
Output is correct |
60 |
Correct |
200 ms |
79944 KB |
Output is correct |
61 |
Correct |
159 ms |
49392 KB |
Output is correct |
62 |
Correct |
169 ms |
46636 KB |
Output is correct |
63 |
Correct |
155 ms |
46664 KB |
Output is correct |
64 |
Correct |
362 ms |
88136 KB |
Output is correct |
65 |
Correct |
337 ms |
86884 KB |
Output is correct |
66 |
Correct |
242 ms |
76172 KB |
Output is correct |
67 |
Correct |
111 ms |
43192 KB |
Output is correct |
68 |
Correct |
271 ms |
59572 KB |
Output is correct |
69 |
Correct |
299 ms |
63412 KB |
Output is correct |
70 |
Correct |
246 ms |
58196 KB |
Output is correct |