#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<n;++i)
#define pi pair<int,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
const int inf=1e9;
int ans=inf;
const int N=2e5+5;
map<int,int> z[N];
int add1[N], add2[N];
vector<pi> adj[N];
int k;
void dfs(int u, int p) {
z[u][0]=0;
for(auto&e:adj[u]) {
int v=e.f, w=e.s;
if (v==p) continue;
dfs(v,u);
add1[v]++; add2[v]+=w;
if (z[v].size()>z[u].size()) {
swap(z[u],z[v]);
swap(add1[u],add1[v]);
swap(add2[u],add2[v]);
}
for(auto&it:z[v]) {
int x=it.f, y=it.s;
//x + add2[v] + add2[u] + len = k
if (z[u].count(k-x-add2[v]-add2[u])) {
ans=min(ans,z[u][k-x-add2[v]-add2[u]]+add1[u]+add1[v]+y);
}
}
for(auto&it:z[v]) {
int x=it.f, y=it.s;
if (z[u].count(x+add2[v]-add2[u])) {
z[u][x+add2[v]-add2[u]]=min(z[u][x+add2[v]-add2[u]],y+add1[v]-add1[u]);
} else {
z[u][x+add2[v]-add2[u]]=y+add1[v]-add1[u];
}
}
}
}
int best_path(int n, int _k, int h[][2], int l[]) {
k=_k;
forn(i,n-1) {
int u=h[i][0], v=h[i][1];
adj[u].pb({v,l[i]}); adj[v].pb({u,l[i]});
}
dfs(0,-1);
if (ans==inf) return -1;
else return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14400 KB |
Output is correct |
4 |
Correct |
6 ms |
14408 KB |
Output is correct |
5 |
Correct |
7 ms |
14348 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14408 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14412 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14336 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14404 KB |
Output is correct |
16 |
Correct |
6 ms |
14408 KB |
Output is correct |
17 |
Correct |
7 ms |
14404 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14400 KB |
Output is correct |
4 |
Correct |
6 ms |
14408 KB |
Output is correct |
5 |
Correct |
7 ms |
14348 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14408 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14412 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14336 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14404 KB |
Output is correct |
16 |
Correct |
6 ms |
14408 KB |
Output is correct |
17 |
Correct |
7 ms |
14404 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
6 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14408 KB |
Output is correct |
21 |
Correct |
7 ms |
14548 KB |
Output is correct |
22 |
Correct |
8 ms |
14676 KB |
Output is correct |
23 |
Correct |
7 ms |
14628 KB |
Output is correct |
24 |
Correct |
8 ms |
14548 KB |
Output is correct |
25 |
Correct |
7 ms |
14720 KB |
Output is correct |
26 |
Correct |
10 ms |
14548 KB |
Output is correct |
27 |
Correct |
7 ms |
14548 KB |
Output is correct |
28 |
Correct |
8 ms |
14548 KB |
Output is correct |
29 |
Correct |
8 ms |
14544 KB |
Output is correct |
30 |
Correct |
8 ms |
14548 KB |
Output is correct |
31 |
Correct |
7 ms |
14548 KB |
Output is correct |
32 |
Correct |
7 ms |
14552 KB |
Output is correct |
33 |
Correct |
7 ms |
14676 KB |
Output is correct |
34 |
Correct |
8 ms |
14592 KB |
Output is correct |
35 |
Correct |
7 ms |
14540 KB |
Output is correct |
36 |
Correct |
8 ms |
14548 KB |
Output is correct |
37 |
Correct |
7 ms |
14548 KB |
Output is correct |
38 |
Correct |
7 ms |
14536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14400 KB |
Output is correct |
4 |
Correct |
6 ms |
14408 KB |
Output is correct |
5 |
Correct |
7 ms |
14348 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14408 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14412 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14336 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14404 KB |
Output is correct |
16 |
Correct |
6 ms |
14408 KB |
Output is correct |
17 |
Correct |
7 ms |
14404 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
117 ms |
33000 KB |
Output is correct |
20 |
Correct |
109 ms |
33056 KB |
Output is correct |
21 |
Correct |
90 ms |
32844 KB |
Output is correct |
22 |
Correct |
98 ms |
32628 KB |
Output is correct |
23 |
Correct |
142 ms |
42208 KB |
Output is correct |
24 |
Correct |
121 ms |
35112 KB |
Output is correct |
25 |
Correct |
87 ms |
34304 KB |
Output is correct |
26 |
Correct |
49 ms |
41192 KB |
Output is correct |
27 |
Correct |
141 ms |
43100 KB |
Output is correct |
28 |
Correct |
214 ms |
77644 KB |
Output is correct |
29 |
Correct |
221 ms |
76036 KB |
Output is correct |
30 |
Correct |
150 ms |
43096 KB |
Output is correct |
31 |
Correct |
148 ms |
43084 KB |
Output is correct |
32 |
Correct |
191 ms |
43208 KB |
Output is correct |
33 |
Correct |
196 ms |
46544 KB |
Output is correct |
34 |
Correct |
292 ms |
71456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14400 KB |
Output is correct |
4 |
Correct |
6 ms |
14408 KB |
Output is correct |
5 |
Correct |
7 ms |
14348 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14408 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14412 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14336 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14404 KB |
Output is correct |
16 |
Correct |
6 ms |
14408 KB |
Output is correct |
17 |
Correct |
7 ms |
14404 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
6 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14408 KB |
Output is correct |
21 |
Correct |
7 ms |
14548 KB |
Output is correct |
22 |
Correct |
8 ms |
14676 KB |
Output is correct |
23 |
Correct |
7 ms |
14628 KB |
Output is correct |
24 |
Correct |
8 ms |
14548 KB |
Output is correct |
25 |
Correct |
7 ms |
14720 KB |
Output is correct |
26 |
Correct |
10 ms |
14548 KB |
Output is correct |
27 |
Correct |
7 ms |
14548 KB |
Output is correct |
28 |
Correct |
8 ms |
14548 KB |
Output is correct |
29 |
Correct |
8 ms |
14544 KB |
Output is correct |
30 |
Correct |
8 ms |
14548 KB |
Output is correct |
31 |
Correct |
7 ms |
14548 KB |
Output is correct |
32 |
Correct |
7 ms |
14552 KB |
Output is correct |
33 |
Correct |
7 ms |
14676 KB |
Output is correct |
34 |
Correct |
8 ms |
14592 KB |
Output is correct |
35 |
Correct |
7 ms |
14540 KB |
Output is correct |
36 |
Correct |
8 ms |
14548 KB |
Output is correct |
37 |
Correct |
7 ms |
14548 KB |
Output is correct |
38 |
Correct |
7 ms |
14536 KB |
Output is correct |
39 |
Correct |
117 ms |
33000 KB |
Output is correct |
40 |
Correct |
109 ms |
33056 KB |
Output is correct |
41 |
Correct |
90 ms |
32844 KB |
Output is correct |
42 |
Correct |
98 ms |
32628 KB |
Output is correct |
43 |
Correct |
142 ms |
42208 KB |
Output is correct |
44 |
Correct |
121 ms |
35112 KB |
Output is correct |
45 |
Correct |
87 ms |
34304 KB |
Output is correct |
46 |
Correct |
49 ms |
41192 KB |
Output is correct |
47 |
Correct |
141 ms |
43100 KB |
Output is correct |
48 |
Correct |
214 ms |
77644 KB |
Output is correct |
49 |
Correct |
221 ms |
76036 KB |
Output is correct |
50 |
Correct |
150 ms |
43096 KB |
Output is correct |
51 |
Correct |
148 ms |
43084 KB |
Output is correct |
52 |
Correct |
191 ms |
43208 KB |
Output is correct |
53 |
Correct |
196 ms |
46544 KB |
Output is correct |
54 |
Correct |
292 ms |
71456 KB |
Output is correct |
55 |
Correct |
24 ms |
16924 KB |
Output is correct |
56 |
Correct |
12 ms |
15956 KB |
Output is correct |
57 |
Correct |
67 ms |
31148 KB |
Output is correct |
58 |
Correct |
52 ms |
26044 KB |
Output is correct |
59 |
Correct |
84 ms |
45780 KB |
Output is correct |
60 |
Correct |
220 ms |
76420 KB |
Output is correct |
61 |
Correct |
216 ms |
45712 KB |
Output is correct |
62 |
Correct |
215 ms |
43112 KB |
Output is correct |
63 |
Correct |
213 ms |
43216 KB |
Output is correct |
64 |
Correct |
424 ms |
84628 KB |
Output is correct |
65 |
Correct |
405 ms |
83348 KB |
Output is correct |
66 |
Correct |
257 ms |
72424 KB |
Output is correct |
67 |
Correct |
194 ms |
39128 KB |
Output is correct |
68 |
Correct |
311 ms |
56024 KB |
Output is correct |
69 |
Correct |
304 ms |
59928 KB |
Output is correct |
70 |
Correct |
283 ms |
54380 KB |
Output is correct |