#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 2e5 + 7;
const ll oo = 1e9 + 69;
int n, k, w[maxn], u, v, ed[maxn][2], dep[maxn], ans;
ll f[maxn];
vector<pa> g[maxn];
unordered_map<ll, int> mp[maxn];
void dfs(int u, int par) {
for(auto e:g[u]) {
int v = e.fi, w = e.se;
if(v == par) continue;
f[v] = f[u] + w;
dep[v] = dep[u] + 1;
dfs(v, u);
}
}
void dfs2(int u, int par) {
mp[u][f[u]] = dep[u];
if(f[u] == k) mini(ans, dep[u]);
for(auto e:g[u]) {
int v = e.fi;
if(v == par) continue;
dfs2(v, u);
if(Size(mp[u]) < Size(mp[v])) {
swap(mp[u], mp[v]);
}
for(auto e:mp[v]) {
ll rem = k + 2LL * f[u] - e.fi;
if(mp[u].count(rem)) mini(ans, e.se + mp[u][rem] - 2 * dep[u]);
}
for(auto e:mp[v]) {
if(!mp[u].count(e.fi)) mp[u][e.fi] = e.se;
else mini(mp[u][e.fi], e.se);
}
}
}
int best_path(int n, int len, int ed[][2], int w[]) {
// cin>>n>>k;
// for(int i = 0; i < n - 1; i++) {
// cin>>ed[i][0]>>ed[i][1];
// }
k = len;
for(int i = 0; i < n - 1; i++) {
// cin>>w[i];
u = ed[i][0] + 1, v = ed[i][1] + 1;
g[u].pb({v, w[i]});
g[v].pb({u, w[i]});
}
ans = oo;
dfs(1, 0);
dfs2(1, 0);
return (ans == oo ? -1:ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
8 ms |
15956 KB |
Output is correct |
3 |
Correct |
9 ms |
15956 KB |
Output is correct |
4 |
Correct |
11 ms |
15956 KB |
Output is correct |
5 |
Correct |
11 ms |
15992 KB |
Output is correct |
6 |
Correct |
10 ms |
15956 KB |
Output is correct |
7 |
Correct |
10 ms |
15956 KB |
Output is correct |
8 |
Correct |
9 ms |
15956 KB |
Output is correct |
9 |
Correct |
9 ms |
15956 KB |
Output is correct |
10 |
Correct |
9 ms |
15956 KB |
Output is correct |
11 |
Correct |
9 ms |
15920 KB |
Output is correct |
12 |
Correct |
9 ms |
15956 KB |
Output is correct |
13 |
Correct |
10 ms |
15956 KB |
Output is correct |
14 |
Correct |
12 ms |
16020 KB |
Output is correct |
15 |
Correct |
11 ms |
15956 KB |
Output is correct |
16 |
Correct |
9 ms |
15956 KB |
Output is correct |
17 |
Correct |
9 ms |
16024 KB |
Output is correct |
18 |
Correct |
9 ms |
15968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
8 ms |
15956 KB |
Output is correct |
3 |
Correct |
9 ms |
15956 KB |
Output is correct |
4 |
Correct |
11 ms |
15956 KB |
Output is correct |
5 |
Correct |
11 ms |
15992 KB |
Output is correct |
6 |
Correct |
10 ms |
15956 KB |
Output is correct |
7 |
Correct |
10 ms |
15956 KB |
Output is correct |
8 |
Correct |
9 ms |
15956 KB |
Output is correct |
9 |
Correct |
9 ms |
15956 KB |
Output is correct |
10 |
Correct |
9 ms |
15956 KB |
Output is correct |
11 |
Correct |
9 ms |
15920 KB |
Output is correct |
12 |
Correct |
9 ms |
15956 KB |
Output is correct |
13 |
Correct |
10 ms |
15956 KB |
Output is correct |
14 |
Correct |
12 ms |
16020 KB |
Output is correct |
15 |
Correct |
11 ms |
15956 KB |
Output is correct |
16 |
Correct |
9 ms |
15956 KB |
Output is correct |
17 |
Correct |
9 ms |
16024 KB |
Output is correct |
18 |
Correct |
9 ms |
15968 KB |
Output is correct |
19 |
Correct |
12 ms |
15996 KB |
Output is correct |
20 |
Correct |
9 ms |
15956 KB |
Output is correct |
21 |
Correct |
10 ms |
16212 KB |
Output is correct |
22 |
Correct |
14 ms |
16284 KB |
Output is correct |
23 |
Correct |
10 ms |
16280 KB |
Output is correct |
24 |
Correct |
10 ms |
16212 KB |
Output is correct |
25 |
Correct |
10 ms |
16212 KB |
Output is correct |
26 |
Correct |
13 ms |
16256 KB |
Output is correct |
27 |
Correct |
13 ms |
16184 KB |
Output is correct |
28 |
Correct |
11 ms |
16212 KB |
Output is correct |
29 |
Correct |
10 ms |
16212 KB |
Output is correct |
30 |
Correct |
11 ms |
16240 KB |
Output is correct |
31 |
Correct |
13 ms |
16212 KB |
Output is correct |
32 |
Correct |
11 ms |
16224 KB |
Output is correct |
33 |
Correct |
14 ms |
16320 KB |
Output is correct |
34 |
Correct |
12 ms |
16232 KB |
Output is correct |
35 |
Correct |
12 ms |
16236 KB |
Output is correct |
36 |
Correct |
10 ms |
16212 KB |
Output is correct |
37 |
Correct |
9 ms |
16236 KB |
Output is correct |
38 |
Correct |
11 ms |
16232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
8 ms |
15956 KB |
Output is correct |
3 |
Correct |
9 ms |
15956 KB |
Output is correct |
4 |
Correct |
11 ms |
15956 KB |
Output is correct |
5 |
Correct |
11 ms |
15992 KB |
Output is correct |
6 |
Correct |
10 ms |
15956 KB |
Output is correct |
7 |
Correct |
10 ms |
15956 KB |
Output is correct |
8 |
Correct |
9 ms |
15956 KB |
Output is correct |
9 |
Correct |
9 ms |
15956 KB |
Output is correct |
10 |
Correct |
9 ms |
15956 KB |
Output is correct |
11 |
Correct |
9 ms |
15920 KB |
Output is correct |
12 |
Correct |
9 ms |
15956 KB |
Output is correct |
13 |
Correct |
10 ms |
15956 KB |
Output is correct |
14 |
Correct |
12 ms |
16020 KB |
Output is correct |
15 |
Correct |
11 ms |
15956 KB |
Output is correct |
16 |
Correct |
9 ms |
15956 KB |
Output is correct |
17 |
Correct |
9 ms |
16024 KB |
Output is correct |
18 |
Correct |
9 ms |
15968 KB |
Output is correct |
19 |
Correct |
140 ms |
41556 KB |
Output is correct |
20 |
Correct |
126 ms |
42888 KB |
Output is correct |
21 |
Correct |
137 ms |
42660 KB |
Output is correct |
22 |
Correct |
161 ms |
42424 KB |
Output is correct |
23 |
Correct |
160 ms |
50980 KB |
Output is correct |
24 |
Correct |
130 ms |
44700 KB |
Output is correct |
25 |
Correct |
145 ms |
45488 KB |
Output is correct |
26 |
Correct |
85 ms |
52496 KB |
Output is correct |
27 |
Correct |
250 ms |
63688 KB |
Output is correct |
28 |
Correct |
349 ms |
98624 KB |
Output is correct |
29 |
Correct |
343 ms |
97224 KB |
Output is correct |
30 |
Correct |
246 ms |
63692 KB |
Output is correct |
31 |
Correct |
236 ms |
63644 KB |
Output is correct |
32 |
Correct |
354 ms |
63776 KB |
Output is correct |
33 |
Correct |
244 ms |
66844 KB |
Output is correct |
34 |
Correct |
426 ms |
89784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
8 ms |
15956 KB |
Output is correct |
3 |
Correct |
9 ms |
15956 KB |
Output is correct |
4 |
Correct |
11 ms |
15956 KB |
Output is correct |
5 |
Correct |
11 ms |
15992 KB |
Output is correct |
6 |
Correct |
10 ms |
15956 KB |
Output is correct |
7 |
Correct |
10 ms |
15956 KB |
Output is correct |
8 |
Correct |
9 ms |
15956 KB |
Output is correct |
9 |
Correct |
9 ms |
15956 KB |
Output is correct |
10 |
Correct |
9 ms |
15956 KB |
Output is correct |
11 |
Correct |
9 ms |
15920 KB |
Output is correct |
12 |
Correct |
9 ms |
15956 KB |
Output is correct |
13 |
Correct |
10 ms |
15956 KB |
Output is correct |
14 |
Correct |
12 ms |
16020 KB |
Output is correct |
15 |
Correct |
11 ms |
15956 KB |
Output is correct |
16 |
Correct |
9 ms |
15956 KB |
Output is correct |
17 |
Correct |
9 ms |
16024 KB |
Output is correct |
18 |
Correct |
9 ms |
15968 KB |
Output is correct |
19 |
Correct |
12 ms |
15996 KB |
Output is correct |
20 |
Correct |
9 ms |
15956 KB |
Output is correct |
21 |
Correct |
10 ms |
16212 KB |
Output is correct |
22 |
Correct |
14 ms |
16284 KB |
Output is correct |
23 |
Correct |
10 ms |
16280 KB |
Output is correct |
24 |
Correct |
10 ms |
16212 KB |
Output is correct |
25 |
Correct |
10 ms |
16212 KB |
Output is correct |
26 |
Correct |
13 ms |
16256 KB |
Output is correct |
27 |
Correct |
13 ms |
16184 KB |
Output is correct |
28 |
Correct |
11 ms |
16212 KB |
Output is correct |
29 |
Correct |
10 ms |
16212 KB |
Output is correct |
30 |
Correct |
11 ms |
16240 KB |
Output is correct |
31 |
Correct |
13 ms |
16212 KB |
Output is correct |
32 |
Correct |
11 ms |
16224 KB |
Output is correct |
33 |
Correct |
14 ms |
16320 KB |
Output is correct |
34 |
Correct |
12 ms |
16232 KB |
Output is correct |
35 |
Correct |
12 ms |
16236 KB |
Output is correct |
36 |
Correct |
10 ms |
16212 KB |
Output is correct |
37 |
Correct |
9 ms |
16236 KB |
Output is correct |
38 |
Correct |
11 ms |
16232 KB |
Output is correct |
39 |
Correct |
140 ms |
41556 KB |
Output is correct |
40 |
Correct |
126 ms |
42888 KB |
Output is correct |
41 |
Correct |
137 ms |
42660 KB |
Output is correct |
42 |
Correct |
161 ms |
42424 KB |
Output is correct |
43 |
Correct |
160 ms |
50980 KB |
Output is correct |
44 |
Correct |
130 ms |
44700 KB |
Output is correct |
45 |
Correct |
145 ms |
45488 KB |
Output is correct |
46 |
Correct |
85 ms |
52496 KB |
Output is correct |
47 |
Correct |
250 ms |
63688 KB |
Output is correct |
48 |
Correct |
349 ms |
98624 KB |
Output is correct |
49 |
Correct |
343 ms |
97224 KB |
Output is correct |
50 |
Correct |
246 ms |
63692 KB |
Output is correct |
51 |
Correct |
236 ms |
63644 KB |
Output is correct |
52 |
Correct |
354 ms |
63776 KB |
Output is correct |
53 |
Correct |
244 ms |
66844 KB |
Output is correct |
54 |
Correct |
426 ms |
89784 KB |
Output is correct |
55 |
Correct |
24 ms |
19276 KB |
Output is correct |
56 |
Correct |
19 ms |
18336 KB |
Output is correct |
57 |
Correct |
89 ms |
40968 KB |
Output is correct |
58 |
Correct |
63 ms |
37352 KB |
Output is correct |
59 |
Correct |
91 ms |
57184 KB |
Output is correct |
60 |
Correct |
334 ms |
97628 KB |
Output is correct |
61 |
Correct |
258 ms |
66100 KB |
Output is correct |
62 |
Correct |
210 ms |
63592 KB |
Output is correct |
63 |
Correct |
328 ms |
63720 KB |
Output is correct |
64 |
Correct |
483 ms |
99788 KB |
Output is correct |
65 |
Correct |
557 ms |
99084 KB |
Output is correct |
66 |
Correct |
345 ms |
93664 KB |
Output is correct |
67 |
Correct |
220 ms |
60224 KB |
Output is correct |
68 |
Correct |
460 ms |
74744 KB |
Output is correct |
69 |
Correct |
471 ms |
78240 KB |
Output is correct |
70 |
Correct |
411 ms |
72216 KB |
Output is correct |