#include "race.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define END "\n"
#define rall(v) (v).rbegin(), (v).rend()
#define all(v) (v).begin(), (v).end()
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int best_path(int n, int k, int H[][2], int L[]) {
vector<vector<pii>> adj(n);
for (int i = 0; i < n - 1; ++i) {
adj[H[i][0]].push_back({H[i][1], L[i]});
adj[H[i][1]].push_back({H[i][0], L[i]});
}
int ans = INT_MAX;
function<unordered_map<ll, int, custom_hash>(int, int, int, ll)> dfs = [&](int v, int p, int d, ll l) -> unordered_map<ll, int, custom_hash> {
unordered_map<ll, int, custom_hash> mp;
mp[l] = d;
for (auto u : adj[v]) {
if (u.ff != p) {
auto ret = dfs(u.ff, v, d + 1, l + u.ss);
// cout << "RET: " << v << " " << u.ff << END;
// for (auto u: ret) cout << u.ff << " " << u.ss << END;
// cout << END;
if (mp.size() < ret.size()) swap(mp, ret);
for (auto x : ret) {
if (mp.find(k - x.ff + l * 2) != mp.end()) {
int pp = mp[k - x.ff + l * 2];
// cout << "In " << v << ": " << u.ff << " " << x.ff << " " << x.ss << " " << k - x.ff + l * 2 << " " << pp << " " << d << " " << l << END;
ans = min(ans, pp + x.ss - 2 * d);
}
}
for (auto x : ret) {
// k = a + b - v * 2
// cout << "In " << v << ": " << u.ff << " " << x.ff << " " << x.ss << END;
// if (mp.find(k - x.ff + l * 2) != mp.end()) {
// int pp = mp[k - x.ff + l * 2];
// cout << "In " << v << ": " << u.ff << " " << x.ff << " " << x.ss << " " << k - x.ff + l * 2 << " " << pp << " " << d << " " << l << END;
// ans = min(ans, pp + x.ss - 2 * d);
// }
if (mp.find(x.ff) == mp.end())
mp[x.ff] = x.ss;
else
mp[x.ff] = min(mp[x.ff], x.ss);
}
}
}
return mp;
};
dfs(0, -1, 0, 0);
return ans == INT_MAX ? -1 : ans;
// return n;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
110 ms |
7624 KB |
Output is correct |
20 |
Correct |
106 ms |
7600 KB |
Output is correct |
21 |
Correct |
104 ms |
7472 KB |
Output is correct |
22 |
Correct |
119 ms |
7724 KB |
Output is correct |
23 |
Correct |
152 ms |
7976 KB |
Output is correct |
24 |
Correct |
108 ms |
7692 KB |
Output is correct |
25 |
Correct |
91 ms |
27176 KB |
Output is correct |
26 |
Correct |
90 ms |
44676 KB |
Output is correct |
27 |
Correct |
230 ms |
15680 KB |
Output is correct |
28 |
Correct |
366 ms |
92912 KB |
Output is correct |
29 |
Correct |
330 ms |
88460 KB |
Output is correct |
30 |
Correct |
183 ms |
15560 KB |
Output is correct |
31 |
Correct |
191 ms |
15656 KB |
Output is correct |
32 |
Correct |
195 ms |
15936 KB |
Output is correct |
33 |
Correct |
235 ms |
15432 KB |
Output is correct |
34 |
Correct |
346 ms |
29092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
110 ms |
7624 KB |
Output is correct |
40 |
Correct |
106 ms |
7600 KB |
Output is correct |
41 |
Correct |
104 ms |
7472 KB |
Output is correct |
42 |
Correct |
119 ms |
7724 KB |
Output is correct |
43 |
Correct |
152 ms |
7976 KB |
Output is correct |
44 |
Correct |
108 ms |
7692 KB |
Output is correct |
45 |
Correct |
91 ms |
27176 KB |
Output is correct |
46 |
Correct |
90 ms |
44676 KB |
Output is correct |
47 |
Correct |
230 ms |
15680 KB |
Output is correct |
48 |
Correct |
366 ms |
92912 KB |
Output is correct |
49 |
Correct |
330 ms |
88460 KB |
Output is correct |
50 |
Correct |
183 ms |
15560 KB |
Output is correct |
51 |
Correct |
191 ms |
15656 KB |
Output is correct |
52 |
Correct |
195 ms |
15936 KB |
Output is correct |
53 |
Correct |
235 ms |
15432 KB |
Output is correct |
54 |
Correct |
346 ms |
29092 KB |
Output is correct |
55 |
Correct |
14 ms |
1620 KB |
Output is correct |
56 |
Correct |
12 ms |
1072 KB |
Output is correct |
57 |
Correct |
77 ms |
7980 KB |
Output is correct |
58 |
Correct |
60 ms |
7968 KB |
Output is correct |
59 |
Correct |
122 ms |
46892 KB |
Output is correct |
60 |
Correct |
333 ms |
90236 KB |
Output is correct |
61 |
Correct |
195 ms |
17228 KB |
Output is correct |
62 |
Correct |
173 ms |
15648 KB |
Output is correct |
63 |
Correct |
237 ms |
15684 KB |
Output is correct |
64 |
Correct |
390 ms |
23268 KB |
Output is correct |
65 |
Correct |
404 ms |
28180 KB |
Output is correct |
66 |
Correct |
349 ms |
78124 KB |
Output is correct |
67 |
Correct |
134 ms |
15420 KB |
Output is correct |
68 |
Correct |
299 ms |
24680 KB |
Output is correct |
69 |
Correct |
280 ms |
28776 KB |
Output is correct |
70 |
Correct |
287 ms |
23900 KB |
Output is correct |