// In the name of God
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 2e5 + 5;
const int inf = 1e9 + 7;
ll n, k, ans = inf;
vector<pii> adj[maxn];
map<ll, ll> mn[maxn];
void dfs(int v = 0, int h = 0, ll d = 0, int p = -1){
mn[v].insert(mp(d, h));
for(auto [u, w]: adj[v]){
if(u == p) continue;
dfs(u, h + 1, d + w, v);
if(mn[u].size() > mn[v].size()) swap(mn[u], mn[v]);
for(auto w: mn[u]){
ll x = k + 2 * d - w.F;
auto it = mn[v].find(x);
if(it != mn[v].end()) smin(ans, w.S + (*it).S - 2 * h);
}
for(auto w: mn[u]){
auto it = mn[v].find(w.F);
if(it != mn[v].end()) smin((*it).S, w.S);
else mn[v].insert(w);
}
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N, k = K;
for(int i = 0; i < n - 1; i++){
int u = H[i][0], v = H[i][1], w = L[i];
adj[u].pb(mp(v, w));
adj[v].pb(mp(u, w));
}
dfs();
return (ans >= inf ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
18012 KB |
Output is correct |
2 |
Correct |
5 ms |
18012 KB |
Output is correct |
3 |
Correct |
5 ms |
18012 KB |
Output is correct |
4 |
Correct |
6 ms |
18012 KB |
Output is correct |
5 |
Correct |
5 ms |
18264 KB |
Output is correct |
6 |
Correct |
5 ms |
18012 KB |
Output is correct |
7 |
Correct |
5 ms |
18012 KB |
Output is correct |
8 |
Correct |
5 ms |
18008 KB |
Output is correct |
9 |
Correct |
6 ms |
18008 KB |
Output is correct |
10 |
Correct |
6 ms |
18012 KB |
Output is correct |
11 |
Correct |
6 ms |
18008 KB |
Output is correct |
12 |
Correct |
5 ms |
18012 KB |
Output is correct |
13 |
Correct |
6 ms |
18012 KB |
Output is correct |
14 |
Correct |
5 ms |
18012 KB |
Output is correct |
15 |
Correct |
6 ms |
18268 KB |
Output is correct |
16 |
Correct |
6 ms |
18008 KB |
Output is correct |
17 |
Correct |
5 ms |
18012 KB |
Output is correct |
18 |
Correct |
6 ms |
18012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
18012 KB |
Output is correct |
2 |
Correct |
5 ms |
18012 KB |
Output is correct |
3 |
Correct |
5 ms |
18012 KB |
Output is correct |
4 |
Correct |
6 ms |
18012 KB |
Output is correct |
5 |
Correct |
5 ms |
18264 KB |
Output is correct |
6 |
Correct |
5 ms |
18012 KB |
Output is correct |
7 |
Correct |
5 ms |
18012 KB |
Output is correct |
8 |
Correct |
5 ms |
18008 KB |
Output is correct |
9 |
Correct |
6 ms |
18008 KB |
Output is correct |
10 |
Correct |
6 ms |
18012 KB |
Output is correct |
11 |
Correct |
6 ms |
18008 KB |
Output is correct |
12 |
Correct |
5 ms |
18012 KB |
Output is correct |
13 |
Correct |
6 ms |
18012 KB |
Output is correct |
14 |
Correct |
5 ms |
18012 KB |
Output is correct |
15 |
Correct |
6 ms |
18268 KB |
Output is correct |
16 |
Correct |
6 ms |
18008 KB |
Output is correct |
17 |
Correct |
5 ms |
18012 KB |
Output is correct |
18 |
Correct |
6 ms |
18012 KB |
Output is correct |
19 |
Correct |
5 ms |
18012 KB |
Output is correct |
20 |
Correct |
6 ms |
18008 KB |
Output is correct |
21 |
Correct |
6 ms |
18268 KB |
Output is correct |
22 |
Correct |
6 ms |
18268 KB |
Output is correct |
23 |
Correct |
6 ms |
18268 KB |
Output is correct |
24 |
Correct |
7 ms |
18268 KB |
Output is correct |
25 |
Correct |
6 ms |
18268 KB |
Output is correct |
26 |
Correct |
6 ms |
18268 KB |
Output is correct |
27 |
Correct |
5 ms |
18268 KB |
Output is correct |
28 |
Correct |
6 ms |
18268 KB |
Output is correct |
29 |
Correct |
6 ms |
18268 KB |
Output is correct |
30 |
Correct |
6 ms |
18480 KB |
Output is correct |
31 |
Correct |
7 ms |
18356 KB |
Output is correct |
32 |
Correct |
6 ms |
18264 KB |
Output is correct |
33 |
Correct |
6 ms |
18280 KB |
Output is correct |
34 |
Correct |
5 ms |
18268 KB |
Output is correct |
35 |
Correct |
5 ms |
18268 KB |
Output is correct |
36 |
Correct |
6 ms |
18268 KB |
Output is correct |
37 |
Correct |
7 ms |
18268 KB |
Output is correct |
38 |
Correct |
7 ms |
18268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
18012 KB |
Output is correct |
2 |
Correct |
5 ms |
18012 KB |
Output is correct |
3 |
Correct |
5 ms |
18012 KB |
Output is correct |
4 |
Correct |
6 ms |
18012 KB |
Output is correct |
5 |
Correct |
5 ms |
18264 KB |
Output is correct |
6 |
Correct |
5 ms |
18012 KB |
Output is correct |
7 |
Correct |
5 ms |
18012 KB |
Output is correct |
8 |
Correct |
5 ms |
18008 KB |
Output is correct |
9 |
Correct |
6 ms |
18008 KB |
Output is correct |
10 |
Correct |
6 ms |
18012 KB |
Output is correct |
11 |
Correct |
6 ms |
18008 KB |
Output is correct |
12 |
Correct |
5 ms |
18012 KB |
Output is correct |
13 |
Correct |
6 ms |
18012 KB |
Output is correct |
14 |
Correct |
5 ms |
18012 KB |
Output is correct |
15 |
Correct |
6 ms |
18268 KB |
Output is correct |
16 |
Correct |
6 ms |
18008 KB |
Output is correct |
17 |
Correct |
5 ms |
18012 KB |
Output is correct |
18 |
Correct |
6 ms |
18012 KB |
Output is correct |
19 |
Correct |
114 ms |
39520 KB |
Output is correct |
20 |
Correct |
115 ms |
40020 KB |
Output is correct |
21 |
Correct |
102 ms |
39332 KB |
Output is correct |
22 |
Correct |
108 ms |
38832 KB |
Output is correct |
23 |
Correct |
163 ms |
51616 KB |
Output is correct |
24 |
Correct |
129 ms |
42604 KB |
Output is correct |
25 |
Correct |
90 ms |
37744 KB |
Output is correct |
26 |
Correct |
50 ms |
43856 KB |
Output is correct |
27 |
Correct |
191 ms |
46928 KB |
Output is correct |
28 |
Correct |
314 ms |
79700 KB |
Output is correct |
29 |
Correct |
305 ms |
78464 KB |
Output is correct |
30 |
Correct |
163 ms |
47020 KB |
Output is correct |
31 |
Correct |
188 ms |
47044 KB |
Output is correct |
32 |
Correct |
227 ms |
47016 KB |
Output is correct |
33 |
Correct |
196 ms |
51796 KB |
Output is correct |
34 |
Correct |
292 ms |
83860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
18012 KB |
Output is correct |
2 |
Correct |
5 ms |
18012 KB |
Output is correct |
3 |
Correct |
5 ms |
18012 KB |
Output is correct |
4 |
Correct |
6 ms |
18012 KB |
Output is correct |
5 |
Correct |
5 ms |
18264 KB |
Output is correct |
6 |
Correct |
5 ms |
18012 KB |
Output is correct |
7 |
Correct |
5 ms |
18012 KB |
Output is correct |
8 |
Correct |
5 ms |
18008 KB |
Output is correct |
9 |
Correct |
6 ms |
18008 KB |
Output is correct |
10 |
Correct |
6 ms |
18012 KB |
Output is correct |
11 |
Correct |
6 ms |
18008 KB |
Output is correct |
12 |
Correct |
5 ms |
18012 KB |
Output is correct |
13 |
Correct |
6 ms |
18012 KB |
Output is correct |
14 |
Correct |
5 ms |
18012 KB |
Output is correct |
15 |
Correct |
6 ms |
18268 KB |
Output is correct |
16 |
Correct |
6 ms |
18008 KB |
Output is correct |
17 |
Correct |
5 ms |
18012 KB |
Output is correct |
18 |
Correct |
6 ms |
18012 KB |
Output is correct |
19 |
Correct |
5 ms |
18012 KB |
Output is correct |
20 |
Correct |
6 ms |
18008 KB |
Output is correct |
21 |
Correct |
6 ms |
18268 KB |
Output is correct |
22 |
Correct |
6 ms |
18268 KB |
Output is correct |
23 |
Correct |
6 ms |
18268 KB |
Output is correct |
24 |
Correct |
7 ms |
18268 KB |
Output is correct |
25 |
Correct |
6 ms |
18268 KB |
Output is correct |
26 |
Correct |
6 ms |
18268 KB |
Output is correct |
27 |
Correct |
5 ms |
18268 KB |
Output is correct |
28 |
Correct |
6 ms |
18268 KB |
Output is correct |
29 |
Correct |
6 ms |
18268 KB |
Output is correct |
30 |
Correct |
6 ms |
18480 KB |
Output is correct |
31 |
Correct |
7 ms |
18356 KB |
Output is correct |
32 |
Correct |
6 ms |
18264 KB |
Output is correct |
33 |
Correct |
6 ms |
18280 KB |
Output is correct |
34 |
Correct |
5 ms |
18268 KB |
Output is correct |
35 |
Correct |
5 ms |
18268 KB |
Output is correct |
36 |
Correct |
6 ms |
18268 KB |
Output is correct |
37 |
Correct |
7 ms |
18268 KB |
Output is correct |
38 |
Correct |
7 ms |
18268 KB |
Output is correct |
39 |
Correct |
114 ms |
39520 KB |
Output is correct |
40 |
Correct |
115 ms |
40020 KB |
Output is correct |
41 |
Correct |
102 ms |
39332 KB |
Output is correct |
42 |
Correct |
108 ms |
38832 KB |
Output is correct |
43 |
Correct |
163 ms |
51616 KB |
Output is correct |
44 |
Correct |
129 ms |
42604 KB |
Output is correct |
45 |
Correct |
90 ms |
37744 KB |
Output is correct |
46 |
Correct |
50 ms |
43856 KB |
Output is correct |
47 |
Correct |
191 ms |
46928 KB |
Output is correct |
48 |
Correct |
314 ms |
79700 KB |
Output is correct |
49 |
Correct |
305 ms |
78464 KB |
Output is correct |
50 |
Correct |
163 ms |
47020 KB |
Output is correct |
51 |
Correct |
188 ms |
47044 KB |
Output is correct |
52 |
Correct |
227 ms |
47016 KB |
Output is correct |
53 |
Correct |
196 ms |
51796 KB |
Output is correct |
54 |
Correct |
292 ms |
83860 KB |
Output is correct |
55 |
Correct |
17 ms |
21080 KB |
Output is correct |
56 |
Correct |
16 ms |
19804 KB |
Output is correct |
57 |
Correct |
65 ms |
37020 KB |
Output is correct |
58 |
Correct |
41 ms |
30416 KB |
Output is correct |
59 |
Correct |
99 ms |
50004 KB |
Output is correct |
60 |
Correct |
316 ms |
78932 KB |
Output is correct |
61 |
Correct |
180 ms |
50324 KB |
Output is correct |
62 |
Correct |
172 ms |
46908 KB |
Output is correct |
63 |
Correct |
235 ms |
47076 KB |
Output is correct |
64 |
Correct |
402 ms |
101684 KB |
Output is correct |
65 |
Correct |
427 ms |
99672 KB |
Output is correct |
66 |
Correct |
329 ms |
75940 KB |
Output is correct |
67 |
Correct |
141 ms |
40892 KB |
Output is correct |
68 |
Correct |
377 ms |
63640 KB |
Output is correct |
69 |
Correct |
425 ms |
68392 KB |
Output is correct |
70 |
Correct |
344 ms |
61520 KB |
Output is correct |