#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef tuple<int, int, int> iii;
typedef pair<ll, ll> pll;
typedef vector<ii> vii;
typedef vector<ll> vll;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define LSOne(S) ((S) & -(S))
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T>
bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template <class T>
bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
int best_path(int n, int K, int edges[][2], int weights[]) {
vector<vii> AL(n);
for (int i = 0; i < n - 1; i++) {
auto [u, v] = edges[i];
int w = weights[i];
AL[u].push_back({v, w});
AL[v].push_back({u, w});
}
int ans = 1e9;
auto dfs = [&](auto self, int u, int p, int d, ll s) -> map<ll, int> {
map<ll, int> res;
res[s] = d;
ll targ = K + 2 * s;
for (auto [v, w] : AL[u]) {
if (v == p) continue;
auto cur = self(self, v, u, d + 1, s + w);
if (res.size() < cur.size()) swap(res, cur);
for (auto [k, v] : cur) {
if (res.count(targ - k)) ckmin(ans, v + res[targ - k] - 2 * d);
}
for (auto [k, v] : cur) {
if (res.count(k))
ckmin(res[k], v);
else
res[k] = v;
}
}
return res;
};
dfs(dfs, 0, -1, 0, 0);
return ans == 1e9 ? -1 : ans;
}
// int main() {
// ios_base::sync_with_stdio(0), cin.tie(0);
// int tc = 1;
// // cin >> tc;
// while (tc--) {
// solve();
// }
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2488 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2496 KB |
Output is correct |
9 |
Correct |
0 ms |
2500 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2400 KB |
Output is correct |
14 |
Correct |
1 ms |
2404 KB |
Output is correct |
15 |
Correct |
1 ms |
2400 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2404 KB |
Output is correct |
18 |
Correct |
1 ms |
2404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2488 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2496 KB |
Output is correct |
9 |
Correct |
0 ms |
2500 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2400 KB |
Output is correct |
14 |
Correct |
1 ms |
2404 KB |
Output is correct |
15 |
Correct |
1 ms |
2400 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2404 KB |
Output is correct |
18 |
Correct |
1 ms |
2404 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2400 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2648 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2404 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2508 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2656 KB |
Output is correct |
34 |
Correct |
2 ms |
2504 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2488 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2496 KB |
Output is correct |
9 |
Correct |
0 ms |
2500 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2400 KB |
Output is correct |
14 |
Correct |
1 ms |
2404 KB |
Output is correct |
15 |
Correct |
1 ms |
2400 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2404 KB |
Output is correct |
18 |
Correct |
1 ms |
2404 KB |
Output is correct |
19 |
Correct |
55 ms |
12028 KB |
Output is correct |
20 |
Correct |
58 ms |
12168 KB |
Output is correct |
21 |
Correct |
54 ms |
12156 KB |
Output is correct |
22 |
Correct |
68 ms |
12040 KB |
Output is correct |
23 |
Correct |
75 ms |
12596 KB |
Output is correct |
24 |
Correct |
63 ms |
12380 KB |
Output is correct |
25 |
Correct |
67 ms |
30152 KB |
Output is correct |
26 |
Correct |
48 ms |
45968 KB |
Output is correct |
27 |
Correct |
102 ms |
20080 KB |
Output is correct |
28 |
Correct |
184 ms |
87360 KB |
Output is correct |
29 |
Correct |
226 ms |
84168 KB |
Output is correct |
30 |
Correct |
89 ms |
20040 KB |
Output is correct |
31 |
Correct |
91 ms |
20048 KB |
Output is correct |
32 |
Correct |
93 ms |
20300 KB |
Output is correct |
33 |
Correct |
101 ms |
19792 KB |
Output is correct |
34 |
Correct |
193 ms |
33240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2488 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2496 KB |
Output is correct |
9 |
Correct |
0 ms |
2500 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2400 KB |
Output is correct |
14 |
Correct |
1 ms |
2404 KB |
Output is correct |
15 |
Correct |
1 ms |
2400 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2404 KB |
Output is correct |
18 |
Correct |
1 ms |
2404 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2400 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2648 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2404 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2508 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2656 KB |
Output is correct |
34 |
Correct |
2 ms |
2504 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2652 KB |
Output is correct |
39 |
Correct |
55 ms |
12028 KB |
Output is correct |
40 |
Correct |
58 ms |
12168 KB |
Output is correct |
41 |
Correct |
54 ms |
12156 KB |
Output is correct |
42 |
Correct |
68 ms |
12040 KB |
Output is correct |
43 |
Correct |
75 ms |
12596 KB |
Output is correct |
44 |
Correct |
63 ms |
12380 KB |
Output is correct |
45 |
Correct |
67 ms |
30152 KB |
Output is correct |
46 |
Correct |
48 ms |
45968 KB |
Output is correct |
47 |
Correct |
102 ms |
20080 KB |
Output is correct |
48 |
Correct |
184 ms |
87360 KB |
Output is correct |
49 |
Correct |
226 ms |
84168 KB |
Output is correct |
50 |
Correct |
89 ms |
20040 KB |
Output is correct |
51 |
Correct |
91 ms |
20048 KB |
Output is correct |
52 |
Correct |
93 ms |
20300 KB |
Output is correct |
53 |
Correct |
101 ms |
19792 KB |
Output is correct |
54 |
Correct |
193 ms |
33240 KB |
Output is correct |
55 |
Correct |
9 ms |
3672 KB |
Output is correct |
56 |
Correct |
4 ms |
3164 KB |
Output is correct |
57 |
Correct |
33 ms |
12124 KB |
Output is correct |
58 |
Correct |
28 ms |
11972 KB |
Output is correct |
59 |
Correct |
75 ms |
45908 KB |
Output is correct |
60 |
Correct |
172 ms |
85132 KB |
Output is correct |
61 |
Correct |
116 ms |
21844 KB |
Output is correct |
62 |
Correct |
96 ms |
20304 KB |
Output is correct |
63 |
Correct |
100 ms |
20400 KB |
Output is correct |
64 |
Correct |
307 ms |
28756 KB |
Output is correct |
65 |
Correct |
245 ms |
33396 KB |
Output is correct |
66 |
Correct |
189 ms |
76888 KB |
Output is correct |
67 |
Correct |
68 ms |
20676 KB |
Output is correct |
68 |
Correct |
174 ms |
31312 KB |
Output is correct |
69 |
Correct |
204 ms |
35932 KB |
Output is correct |
70 |
Correct |
195 ms |
30284 KB |
Output is correct |