#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 1e6 + 10;
vector <pair <int, int>> ed[N];
ll d1[N], d2[N], k;
ll ans = 1e9;
map <ll, int> mp[N];
// x + y - 2 * v = k
// x + y = k + 2 * v
void dfs(int u, int v) {
mp[u][d1[u]] = d2[u];
ll tot = k + 2 * d1[u];
for (auto x : ed[u]) {
if (x.f == v)
continue;
d2[x.f] = d2[u] + 1;
d1[x.f] = d1[u] + x.s;
dfs(x.f, u);
if (sz(mp[u]) < sz(mp[x.f])) {
swap(mp[u], mp[x.f]);
}
for (auto el : mp[x.f]) {
auto it = mp[u].find(tot - el.f);
if (it != mp[u].end()) {
ans = min(ans, it -> s + el.s - d2[u] * 2);
}
}
for (auto el : mp[x.f]) {
if (mp[u].find(el.f) != mp[u].end()) {
mp[u][el.f] = min(mp[u][el.f], el.s);
} else {
mp[u][el.f] = el.s;
}
}
}
}
int best_path(int n, int len, int H[][2], int L[]) {
k = len;
for (int i = 0; i < n - 1; i++) {
ed[H[i][0]].pb({H[i][1], L[i]});
ed[H[i][1]].pb({H[i][0], L[i]});
}
dfs(0, 0);
if (ans > n)
ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
75600 KB |
Output is correct |
2 |
Correct |
17 ms |
76120 KB |
Output is correct |
3 |
Correct |
16 ms |
75764 KB |
Output is correct |
4 |
Correct |
16 ms |
75856 KB |
Output is correct |
5 |
Correct |
16 ms |
75608 KB |
Output is correct |
6 |
Correct |
16 ms |
75728 KB |
Output is correct |
7 |
Correct |
17 ms |
75612 KB |
Output is correct |
8 |
Correct |
16 ms |
75868 KB |
Output is correct |
9 |
Correct |
16 ms |
75772 KB |
Output is correct |
10 |
Correct |
16 ms |
75612 KB |
Output is correct |
11 |
Correct |
19 ms |
75612 KB |
Output is correct |
12 |
Correct |
17 ms |
75612 KB |
Output is correct |
13 |
Correct |
16 ms |
75612 KB |
Output is correct |
14 |
Correct |
16 ms |
75868 KB |
Output is correct |
15 |
Correct |
16 ms |
75612 KB |
Output is correct |
16 |
Correct |
16 ms |
75864 KB |
Output is correct |
17 |
Correct |
16 ms |
75612 KB |
Output is correct |
18 |
Correct |
16 ms |
75612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
75600 KB |
Output is correct |
2 |
Correct |
17 ms |
76120 KB |
Output is correct |
3 |
Correct |
16 ms |
75764 KB |
Output is correct |
4 |
Correct |
16 ms |
75856 KB |
Output is correct |
5 |
Correct |
16 ms |
75608 KB |
Output is correct |
6 |
Correct |
16 ms |
75728 KB |
Output is correct |
7 |
Correct |
17 ms |
75612 KB |
Output is correct |
8 |
Correct |
16 ms |
75868 KB |
Output is correct |
9 |
Correct |
16 ms |
75772 KB |
Output is correct |
10 |
Correct |
16 ms |
75612 KB |
Output is correct |
11 |
Correct |
19 ms |
75612 KB |
Output is correct |
12 |
Correct |
17 ms |
75612 KB |
Output is correct |
13 |
Correct |
16 ms |
75612 KB |
Output is correct |
14 |
Correct |
16 ms |
75868 KB |
Output is correct |
15 |
Correct |
16 ms |
75612 KB |
Output is correct |
16 |
Correct |
16 ms |
75864 KB |
Output is correct |
17 |
Correct |
16 ms |
75612 KB |
Output is correct |
18 |
Correct |
16 ms |
75612 KB |
Output is correct |
19 |
Correct |
16 ms |
75612 KB |
Output is correct |
20 |
Correct |
16 ms |
75612 KB |
Output is correct |
21 |
Correct |
18 ms |
75868 KB |
Output is correct |
22 |
Correct |
17 ms |
76124 KB |
Output is correct |
23 |
Correct |
17 ms |
75996 KB |
Output is correct |
24 |
Correct |
18 ms |
76124 KB |
Output is correct |
25 |
Correct |
17 ms |
76120 KB |
Output is correct |
26 |
Correct |
17 ms |
76052 KB |
Output is correct |
27 |
Correct |
17 ms |
75868 KB |
Output is correct |
28 |
Correct |
18 ms |
75988 KB |
Output is correct |
29 |
Correct |
18 ms |
76124 KB |
Output is correct |
30 |
Correct |
17 ms |
76124 KB |
Output is correct |
31 |
Correct |
18 ms |
76376 KB |
Output is correct |
32 |
Correct |
17 ms |
75868 KB |
Output is correct |
33 |
Correct |
19 ms |
76124 KB |
Output is correct |
34 |
Correct |
20 ms |
76028 KB |
Output is correct |
35 |
Correct |
17 ms |
75868 KB |
Output is correct |
36 |
Correct |
17 ms |
75868 KB |
Output is correct |
37 |
Correct |
17 ms |
76064 KB |
Output is correct |
38 |
Correct |
18 ms |
75896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
75600 KB |
Output is correct |
2 |
Correct |
17 ms |
76120 KB |
Output is correct |
3 |
Correct |
16 ms |
75764 KB |
Output is correct |
4 |
Correct |
16 ms |
75856 KB |
Output is correct |
5 |
Correct |
16 ms |
75608 KB |
Output is correct |
6 |
Correct |
16 ms |
75728 KB |
Output is correct |
7 |
Correct |
17 ms |
75612 KB |
Output is correct |
8 |
Correct |
16 ms |
75868 KB |
Output is correct |
9 |
Correct |
16 ms |
75772 KB |
Output is correct |
10 |
Correct |
16 ms |
75612 KB |
Output is correct |
11 |
Correct |
19 ms |
75612 KB |
Output is correct |
12 |
Correct |
17 ms |
75612 KB |
Output is correct |
13 |
Correct |
16 ms |
75612 KB |
Output is correct |
14 |
Correct |
16 ms |
75868 KB |
Output is correct |
15 |
Correct |
16 ms |
75612 KB |
Output is correct |
16 |
Correct |
16 ms |
75864 KB |
Output is correct |
17 |
Correct |
16 ms |
75612 KB |
Output is correct |
18 |
Correct |
16 ms |
75612 KB |
Output is correct |
19 |
Correct |
106 ms |
100072 KB |
Output is correct |
20 |
Correct |
109 ms |
100988 KB |
Output is correct |
21 |
Correct |
111 ms |
100492 KB |
Output is correct |
22 |
Correct |
109 ms |
100728 KB |
Output is correct |
23 |
Correct |
157 ms |
113488 KB |
Output is correct |
24 |
Correct |
129 ms |
104332 KB |
Output is correct |
25 |
Correct |
99 ms |
101148 KB |
Output is correct |
26 |
Correct |
64 ms |
108052 KB |
Output is correct |
27 |
Correct |
160 ms |
111324 KB |
Output is correct |
28 |
Correct |
262 ms |
150352 KB |
Output is correct |
29 |
Correct |
254 ms |
149252 KB |
Output is correct |
30 |
Correct |
180 ms |
112212 KB |
Output is correct |
31 |
Correct |
164 ms |
111956 KB |
Output is correct |
32 |
Correct |
239 ms |
112016 KB |
Output is correct |
33 |
Correct |
198 ms |
116856 KB |
Output is correct |
34 |
Correct |
294 ms |
149328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
75600 KB |
Output is correct |
2 |
Correct |
17 ms |
76120 KB |
Output is correct |
3 |
Correct |
16 ms |
75764 KB |
Output is correct |
4 |
Correct |
16 ms |
75856 KB |
Output is correct |
5 |
Correct |
16 ms |
75608 KB |
Output is correct |
6 |
Correct |
16 ms |
75728 KB |
Output is correct |
7 |
Correct |
17 ms |
75612 KB |
Output is correct |
8 |
Correct |
16 ms |
75868 KB |
Output is correct |
9 |
Correct |
16 ms |
75772 KB |
Output is correct |
10 |
Correct |
16 ms |
75612 KB |
Output is correct |
11 |
Correct |
19 ms |
75612 KB |
Output is correct |
12 |
Correct |
17 ms |
75612 KB |
Output is correct |
13 |
Correct |
16 ms |
75612 KB |
Output is correct |
14 |
Correct |
16 ms |
75868 KB |
Output is correct |
15 |
Correct |
16 ms |
75612 KB |
Output is correct |
16 |
Correct |
16 ms |
75864 KB |
Output is correct |
17 |
Correct |
16 ms |
75612 KB |
Output is correct |
18 |
Correct |
16 ms |
75612 KB |
Output is correct |
19 |
Correct |
16 ms |
75612 KB |
Output is correct |
20 |
Correct |
16 ms |
75612 KB |
Output is correct |
21 |
Correct |
18 ms |
75868 KB |
Output is correct |
22 |
Correct |
17 ms |
76124 KB |
Output is correct |
23 |
Correct |
17 ms |
75996 KB |
Output is correct |
24 |
Correct |
18 ms |
76124 KB |
Output is correct |
25 |
Correct |
17 ms |
76120 KB |
Output is correct |
26 |
Correct |
17 ms |
76052 KB |
Output is correct |
27 |
Correct |
17 ms |
75868 KB |
Output is correct |
28 |
Correct |
18 ms |
75988 KB |
Output is correct |
29 |
Correct |
18 ms |
76124 KB |
Output is correct |
30 |
Correct |
17 ms |
76124 KB |
Output is correct |
31 |
Correct |
18 ms |
76376 KB |
Output is correct |
32 |
Correct |
17 ms |
75868 KB |
Output is correct |
33 |
Correct |
19 ms |
76124 KB |
Output is correct |
34 |
Correct |
20 ms |
76028 KB |
Output is correct |
35 |
Correct |
17 ms |
75868 KB |
Output is correct |
36 |
Correct |
17 ms |
75868 KB |
Output is correct |
37 |
Correct |
17 ms |
76064 KB |
Output is correct |
38 |
Correct |
18 ms |
75896 KB |
Output is correct |
39 |
Correct |
106 ms |
100072 KB |
Output is correct |
40 |
Correct |
109 ms |
100988 KB |
Output is correct |
41 |
Correct |
111 ms |
100492 KB |
Output is correct |
42 |
Correct |
109 ms |
100728 KB |
Output is correct |
43 |
Correct |
157 ms |
113488 KB |
Output is correct |
44 |
Correct |
129 ms |
104332 KB |
Output is correct |
45 |
Correct |
99 ms |
101148 KB |
Output is correct |
46 |
Correct |
64 ms |
108052 KB |
Output is correct |
47 |
Correct |
160 ms |
111324 KB |
Output is correct |
48 |
Correct |
262 ms |
150352 KB |
Output is correct |
49 |
Correct |
254 ms |
149252 KB |
Output is correct |
50 |
Correct |
180 ms |
112212 KB |
Output is correct |
51 |
Correct |
164 ms |
111956 KB |
Output is correct |
52 |
Correct |
239 ms |
112016 KB |
Output is correct |
53 |
Correct |
198 ms |
116856 KB |
Output is correct |
54 |
Correct |
294 ms |
149328 KB |
Output is correct |
55 |
Correct |
29 ms |
78684 KB |
Output is correct |
56 |
Correct |
25 ms |
77660 KB |
Output is correct |
57 |
Correct |
76 ms |
97656 KB |
Output is correct |
58 |
Correct |
63 ms |
92100 KB |
Output is correct |
59 |
Correct |
88 ms |
114516 KB |
Output is correct |
60 |
Correct |
255 ms |
149812 KB |
Output is correct |
61 |
Correct |
195 ms |
114572 KB |
Output is correct |
62 |
Correct |
160 ms |
111792 KB |
Output is correct |
63 |
Correct |
253 ms |
112148 KB |
Output is correct |
64 |
Correct |
410 ms |
165512 KB |
Output is correct |
65 |
Correct |
405 ms |
165368 KB |
Output is correct |
66 |
Correct |
267 ms |
143624 KB |
Output is correct |
67 |
Correct |
179 ms |
107200 KB |
Output is correct |
68 |
Correct |
327 ms |
128492 KB |
Output is correct |
69 |
Correct |
359 ms |
131476 KB |
Output is correct |
70 |
Correct |
351 ms |
127060 KB |
Output is correct |