#include <bits/stdc++.h>
#include "race.h"
using namespace std;
const int N_ = 2e5+1;
vector<vector<pair<int,int>>> a;
map<int,int> mp[N_];
int n,k,len[N_],sum[N_],ans = 1e9;
void dfs(int pos,int last,int ln,int sm)
{
len[pos] = ln;
sum[pos] = sm;
mp[pos][sm] = ln;
for(auto x : a[pos])
{
if(x.first == last)continue;
dfs(x.first,pos,ln+1,sm+x.second);
}
return ;
}
void solve(int pos,int last)
{
int K = k + 2 * sum[pos];
for(auto v : a[pos])
{
if(v.first == last)continue;
solve(v.first,pos);
if(mp[v.first].size() > mp[pos].size())swap(mp[v.first],mp[pos]);
for(auto i : mp[v.first])
{
if(mp[pos].find(K-i.first) != mp[pos].end()){
ans = min(ans,mp[pos][K-i.first] + i.second - 2 * len[pos]);
}
}
for(auto i : mp[v.first]){
if(mp[pos].find(i.first) == mp[pos].end()){
mp[pos].insert(i);
}else{
mp[pos][i.first] = min(mp[pos][i.first],i.second);
}
}
}
}
int best_path(int n_, int k_, int h[][2], int l[])
{
n = n_;k = k_;
a.resize(n);
for(int i = 0;i < n-1;i++)
{
int x = h[i][0],y = h[i][1];
a[x].push_back({y,l[i]});
a[y].push_back({x,l[i]});
}
dfs(0,-1,0,0);
solve(0,-1);
if(ans == 1e9)return -1;
else return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9708 KB |
Output is correct |
4 |
Correct |
5 ms |
9704 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9704 KB |
Output is correct |
8 |
Correct |
5 ms |
9704 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9700 KB |
Output is correct |
12 |
Correct |
5 ms |
9704 KB |
Output is correct |
13 |
Correct |
5 ms |
9688 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9708 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
7 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9708 KB |
Output is correct |
4 |
Correct |
5 ms |
9704 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9704 KB |
Output is correct |
8 |
Correct |
5 ms |
9704 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9700 KB |
Output is correct |
12 |
Correct |
5 ms |
9704 KB |
Output is correct |
13 |
Correct |
5 ms |
9688 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9708 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
7 ms |
9684 KB |
Output is correct |
19 |
Correct |
7 ms |
9712 KB |
Output is correct |
20 |
Correct |
5 ms |
9744 KB |
Output is correct |
21 |
Correct |
7 ms |
9836 KB |
Output is correct |
22 |
Correct |
6 ms |
9940 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
8 ms |
9940 KB |
Output is correct |
25 |
Correct |
6 ms |
9972 KB |
Output is correct |
26 |
Correct |
7 ms |
9968 KB |
Output is correct |
27 |
Correct |
6 ms |
9812 KB |
Output is correct |
28 |
Correct |
7 ms |
9972 KB |
Output is correct |
29 |
Correct |
7 ms |
9972 KB |
Output is correct |
30 |
Correct |
6 ms |
9940 KB |
Output is correct |
31 |
Correct |
6 ms |
9940 KB |
Output is correct |
32 |
Correct |
6 ms |
9940 KB |
Output is correct |
33 |
Correct |
6 ms |
9940 KB |
Output is correct |
34 |
Correct |
6 ms |
9976 KB |
Output is correct |
35 |
Correct |
5 ms |
9864 KB |
Output is correct |
36 |
Correct |
7 ms |
9844 KB |
Output is correct |
37 |
Correct |
6 ms |
9940 KB |
Output is correct |
38 |
Correct |
6 ms |
9940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9708 KB |
Output is correct |
4 |
Correct |
5 ms |
9704 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9704 KB |
Output is correct |
8 |
Correct |
5 ms |
9704 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9700 KB |
Output is correct |
12 |
Correct |
5 ms |
9704 KB |
Output is correct |
13 |
Correct |
5 ms |
9688 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9708 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
7 ms |
9684 KB |
Output is correct |
19 |
Correct |
122 ms |
30700 KB |
Output is correct |
20 |
Correct |
157 ms |
30668 KB |
Output is correct |
21 |
Correct |
139 ms |
30432 KB |
Output is correct |
22 |
Correct |
135 ms |
30360 KB |
Output is correct |
23 |
Correct |
213 ms |
39852 KB |
Output is correct |
24 |
Correct |
128 ms |
32832 KB |
Output is correct |
25 |
Correct |
99 ms |
32680 KB |
Output is correct |
26 |
Correct |
61 ms |
40332 KB |
Output is correct |
27 |
Correct |
197 ms |
42988 KB |
Output is correct |
28 |
Correct |
303 ms |
80672 KB |
Output is correct |
29 |
Correct |
339 ms |
77368 KB |
Output is correct |
30 |
Correct |
212 ms |
41712 KB |
Output is correct |
31 |
Correct |
203 ms |
41932 KB |
Output is correct |
32 |
Correct |
302 ms |
41804 KB |
Output is correct |
33 |
Correct |
301 ms |
45256 KB |
Output is correct |
34 |
Correct |
356 ms |
69128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9708 KB |
Output is correct |
4 |
Correct |
5 ms |
9704 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9704 KB |
Output is correct |
8 |
Correct |
5 ms |
9704 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9700 KB |
Output is correct |
12 |
Correct |
5 ms |
9704 KB |
Output is correct |
13 |
Correct |
5 ms |
9688 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9708 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
7 ms |
9684 KB |
Output is correct |
19 |
Correct |
7 ms |
9712 KB |
Output is correct |
20 |
Correct |
5 ms |
9744 KB |
Output is correct |
21 |
Correct |
7 ms |
9836 KB |
Output is correct |
22 |
Correct |
6 ms |
9940 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
8 ms |
9940 KB |
Output is correct |
25 |
Correct |
6 ms |
9972 KB |
Output is correct |
26 |
Correct |
7 ms |
9968 KB |
Output is correct |
27 |
Correct |
6 ms |
9812 KB |
Output is correct |
28 |
Correct |
7 ms |
9972 KB |
Output is correct |
29 |
Correct |
7 ms |
9972 KB |
Output is correct |
30 |
Correct |
6 ms |
9940 KB |
Output is correct |
31 |
Correct |
6 ms |
9940 KB |
Output is correct |
32 |
Correct |
6 ms |
9940 KB |
Output is correct |
33 |
Correct |
6 ms |
9940 KB |
Output is correct |
34 |
Correct |
6 ms |
9976 KB |
Output is correct |
35 |
Correct |
5 ms |
9864 KB |
Output is correct |
36 |
Correct |
7 ms |
9844 KB |
Output is correct |
37 |
Correct |
6 ms |
9940 KB |
Output is correct |
38 |
Correct |
6 ms |
9940 KB |
Output is correct |
39 |
Correct |
122 ms |
30700 KB |
Output is correct |
40 |
Correct |
157 ms |
30668 KB |
Output is correct |
41 |
Correct |
139 ms |
30432 KB |
Output is correct |
42 |
Correct |
135 ms |
30360 KB |
Output is correct |
43 |
Correct |
213 ms |
39852 KB |
Output is correct |
44 |
Correct |
128 ms |
32832 KB |
Output is correct |
45 |
Correct |
99 ms |
32680 KB |
Output is correct |
46 |
Correct |
61 ms |
40332 KB |
Output is correct |
47 |
Correct |
197 ms |
42988 KB |
Output is correct |
48 |
Correct |
303 ms |
80672 KB |
Output is correct |
49 |
Correct |
339 ms |
77368 KB |
Output is correct |
50 |
Correct |
212 ms |
41712 KB |
Output is correct |
51 |
Correct |
203 ms |
41932 KB |
Output is correct |
52 |
Correct |
302 ms |
41804 KB |
Output is correct |
53 |
Correct |
301 ms |
45256 KB |
Output is correct |
54 |
Correct |
356 ms |
69128 KB |
Output is correct |
55 |
Correct |
20 ms |
12500 KB |
Output is correct |
56 |
Correct |
13 ms |
11476 KB |
Output is correct |
57 |
Correct |
72 ms |
28716 KB |
Output is correct |
58 |
Correct |
50 ms |
23636 KB |
Output is correct |
59 |
Correct |
85 ms |
45004 KB |
Output is correct |
60 |
Correct |
288 ms |
78064 KB |
Output is correct |
61 |
Correct |
283 ms |
44216 KB |
Output is correct |
62 |
Correct |
219 ms |
41736 KB |
Output is correct |
63 |
Correct |
288 ms |
41760 KB |
Output is correct |
64 |
Correct |
461 ms |
82748 KB |
Output is correct |
65 |
Correct |
428 ms |
81056 KB |
Output is correct |
66 |
Correct |
312 ms |
73112 KB |
Output is correct |
67 |
Correct |
197 ms |
37352 KB |
Output is correct |
68 |
Correct |
364 ms |
53912 KB |
Output is correct |
69 |
Correct |
379 ms |
57760 KB |
Output is correct |
70 |
Correct |
332 ms |
52304 KB |
Output is correct |