#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define re resize
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin()+1, (x).end()
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)
#define print(x) cout << #x << ": " << x << endl << flush
template<class T> bool ckmin(T&a, T b) { bool B = a > b; a = min(a, b); return B; }
template<class T> bool ckmax(T&a, T b) { bool B = a < b; a = max(a, b); return B; }
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
typedef vector<ii> vii;
typedef map<int, int> mii;
const int inf = 1e9;
const int maxn = 2e5+5;
vii adj[maxn];
int dep[maxn];
int dist[maxn];
int k;
int res = inf;
mii dfs(int u, int p) {
mii mp;
mp[dist[u]] = dep[u];
for(auto [v, c] : adj[u]) if(v != p) {
dep[v] = dep[u] + 1;
dist[v] = dist[u] + c;
mii mp2 = dfs(v, u);
if(mp.size() < mp2.size())
swap(mp, mp2);
for(auto [di, de] : mp2) {
auto it = mp.find(k + 2*dist[u] - di);
if(it != mp.end()) ckmin(res, de + it->ss - 2*dep[u]);
}
for(auto [di, de] : mp2) {
auto it = mp.find(di);
if(it != mp.end()) ckmin(it->ss, de);
else mp[di] = de;
}
}
return mp;
}
int best_path(int n, int K, int h[][2], int l[]) {
k = K;
loop(i, n-1) {
int u = h[i][0], v = h[i][1];
int c = l[i];
adj[u].pb({v, c});
adj[v].pb({u, c});
}
dep[0] = 1;
dist[0] = 1;
dfs(0, 0);
return (res == inf ? -1 : res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
4 ms |
5004 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5004 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5008 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 |
5048 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4932 KB |
Output is correct |
18 |
Correct |
3 ms |
5008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
4 ms |
5004 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5004 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5008 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 |
5048 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4932 KB |
Output is correct |
18 |
Correct |
3 ms |
5008 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
5 ms |
5076 KB |
Output is correct |
24 |
Correct |
5 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5152 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
5 ms |
5020 KB |
Output is correct |
30 |
Correct |
5 ms |
5148 KB |
Output is correct |
31 |
Correct |
4 ms |
5148 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5204 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
4 ms |
5004 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5004 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5008 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 |
5048 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4932 KB |
Output is correct |
18 |
Correct |
3 ms |
5008 KB |
Output is correct |
19 |
Correct |
85 ms |
11956 KB |
Output is correct |
20 |
Correct |
97 ms |
12096 KB |
Output is correct |
21 |
Correct |
91 ms |
12000 KB |
Output is correct |
22 |
Correct |
92 ms |
12080 KB |
Output is correct |
23 |
Correct |
125 ms |
12464 KB |
Output is correct |
24 |
Correct |
101 ms |
12264 KB |
Output is correct |
25 |
Correct |
73 ms |
24820 KB |
Output is correct |
26 |
Correct |
60 ms |
36412 KB |
Output is correct |
27 |
Correct |
175 ms |
19604 KB |
Output is correct |
28 |
Correct |
235 ms |
68124 KB |
Output is correct |
29 |
Correct |
259 ms |
65848 KB |
Output is correct |
30 |
Correct |
185 ms |
19652 KB |
Output is correct |
31 |
Correct |
152 ms |
19644 KB |
Output is correct |
32 |
Correct |
219 ms |
19788 KB |
Output is correct |
33 |
Correct |
152 ms |
19020 KB |
Output is correct |
34 |
Correct |
293 ms |
29332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
4 ms |
5004 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5004 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5008 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 |
5048 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4932 KB |
Output is correct |
18 |
Correct |
3 ms |
5008 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
5 ms |
5076 KB |
Output is correct |
24 |
Correct |
5 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5152 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
5 ms |
5020 KB |
Output is correct |
30 |
Correct |
5 ms |
5148 KB |
Output is correct |
31 |
Correct |
4 ms |
5148 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5204 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
85 ms |
11956 KB |
Output is correct |
40 |
Correct |
97 ms |
12096 KB |
Output is correct |
41 |
Correct |
91 ms |
12000 KB |
Output is correct |
42 |
Correct |
92 ms |
12080 KB |
Output is correct |
43 |
Correct |
125 ms |
12464 KB |
Output is correct |
44 |
Correct |
101 ms |
12264 KB |
Output is correct |
45 |
Correct |
73 ms |
24820 KB |
Output is correct |
46 |
Correct |
60 ms |
36412 KB |
Output is correct |
47 |
Correct |
175 ms |
19604 KB |
Output is correct |
48 |
Correct |
235 ms |
68124 KB |
Output is correct |
49 |
Correct |
259 ms |
65848 KB |
Output is correct |
50 |
Correct |
185 ms |
19652 KB |
Output is correct |
51 |
Correct |
152 ms |
19644 KB |
Output is correct |
52 |
Correct |
219 ms |
19788 KB |
Output is correct |
53 |
Correct |
152 ms |
19020 KB |
Output is correct |
54 |
Correct |
293 ms |
29332 KB |
Output is correct |
55 |
Correct |
20 ms |
5972 KB |
Output is correct |
56 |
Correct |
11 ms |
5716 KB |
Output is correct |
57 |
Correct |
55 ms |
12108 KB |
Output is correct |
58 |
Correct |
40 ms |
11840 KB |
Output is correct |
59 |
Correct |
87 ms |
36488 KB |
Output is correct |
60 |
Correct |
235 ms |
66572 KB |
Output is correct |
61 |
Correct |
224 ms |
21012 KB |
Output is correct |
62 |
Correct |
172 ms |
19696 KB |
Output is correct |
63 |
Correct |
179 ms |
19836 KB |
Output is correct |
64 |
Correct |
366 ms |
26260 KB |
Output is correct |
65 |
Correct |
398 ms |
29716 KB |
Output is correct |
66 |
Correct |
241 ms |
60760 KB |
Output is correct |
67 |
Correct |
131 ms |
20160 KB |
Output is correct |
68 |
Correct |
321 ms |
28256 KB |
Output is correct |
69 |
Correct |
348 ms |
31740 KB |
Output is correct |
70 |
Correct |
263 ms |
27296 KB |
Output is correct |