#include "race.h"
#include <bits/stdc++.h>
#define ff first
#define ss second
typedef long long ll;
using namespace std;
vector<pair<ll,ll> >adj[300000];
long long desired;
long long ans=1e9;
struct dp{
long long val1;
unordered_map<long long,pair<long long,bool> >um;
long long val;
vector<long long>dp;
};
dp tree[300000];
ll sz[300000];
void dfs(ll node,ll par=-1){
sz[node]=1;
for(pair<ll,ll>child:adj[node]){
if(child.first!=par){
dfs(child.first,node);
sz[node]+=sz[child.first];
}
}
}
void solve(ll node,ll par=-1){
ll dis=0;
pair<ll,ll>temp,bigchild={0,-1};
for(pair<ll,ll>child:adj[node]){
if(child.ff==par) continue;
temp={sz[child.ff],child.ff};
if(temp>bigchild){
dis=child.ss;
bigchild=temp;
}
}
if(bigchild.ss!=-1){
solve(bigchild.ss,node);
swap(tree[node],tree[bigchild.ss]);
}
tree[node].dp.push_back(-dis-tree[node].val);
tree[node].val+=dis;
tree[node].val1++;
tree[node].um[-tree[node].val].first=-tree[node].val1;
tree[node].um[-tree[node].val].second=1;
if(tree[node].um[desired-tree[node].val].ss==1){
ans=min(ans,tree[node].um[desired-tree[node].val].first+tree[node].val1);
}
for(pair<ll,ll>child:adj[node]){
if(child.ff==par) continue;
if(child.ff!=bigchild.ss){
long long dist=child.second;
solve(child.ff,node);
for(ll i=0;i<tree[child.ff].dp.size();i++){
long long actual=tree[child.ff].dp[i]+tree[child.ff].val;
long long dis1=tree[child.ff].um[tree[child.ff].dp[i]].first+tree[child.ff].val1;
bool flag=tree[node].um[desired-actual-dist-tree[node].val].second;
long long dis2=tree[node].um[desired-actual-dist-tree[node].val].first+tree[node].val1;
if(flag==1){
ans=min(ans,dis1+dis2+1);
}
}
for(ll i=0;i<tree[child.ff].dp.size();i++){
long long actual=tree[child.ff].dp[i]+tree[child.ff].val;
long long dis1=tree[child.ff].um[tree[child.ff].dp[i]].first+tree[child.ff].val1;
actual+=dist;
actual-=tree[node].val;
if(tree[node].um[actual].ss==1){
tree[node].um[actual].ff=min(tree[node].um[actual].ff,dis1+1-tree[node].val1);
}
else{
tree[node].dp.push_back(actual);
tree[node].um[actual].ff=dis1+1-tree[node].val1;
tree[node].um[actual].ss=1;
}
}
}
}
// cout << tree[node].val << ' ' << node << '\n';
// for(int i=0;i<tree[node].dp.size();i++){
// cout << tree[node].dp[i] << ' ';
// }
// cout << '\n';
}
int best_path(int N, int K, int H[][2], int L[])
{
desired=K;
for(ll i=0;i<(N-1);i++){
adj[(ll)H[i][0]].push_back({(ll)H[i][1],(ll)L[i]});
adj[(ll)H[i][1]].push_back({(ll)H[i][0],(ll)L[i]});
}
dfs((ll)0);
solve((ll)0);
int answer=ans;
return (1000000000==answer?-1:answer);
}
Compilation message
race.cpp: In function 'void solve(ll, ll)':
race.cpp:55:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(ll i=0;i<tree[child.ff].dp.size();i++){
| ~^~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:64:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(ll i=0;i<tree[child.ff].dp.size();i++){
| ~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
35532 KB |
Output is correct |
2 |
Correct |
29 ms |
35532 KB |
Output is correct |
3 |
Correct |
29 ms |
35580 KB |
Output is correct |
4 |
Correct |
29 ms |
35576 KB |
Output is correct |
5 |
Correct |
27 ms |
35508 KB |
Output is correct |
6 |
Correct |
30 ms |
35576 KB |
Output is correct |
7 |
Correct |
29 ms |
35532 KB |
Output is correct |
8 |
Correct |
26 ms |
35544 KB |
Output is correct |
9 |
Correct |
25 ms |
35584 KB |
Output is correct |
10 |
Correct |
24 ms |
35520 KB |
Output is correct |
11 |
Correct |
28 ms |
35488 KB |
Output is correct |
12 |
Correct |
24 ms |
35528 KB |
Output is correct |
13 |
Correct |
25 ms |
35532 KB |
Output is correct |
14 |
Correct |
25 ms |
35476 KB |
Output is correct |
15 |
Correct |
24 ms |
35580 KB |
Output is correct |
16 |
Correct |
24 ms |
35560 KB |
Output is correct |
17 |
Correct |
26 ms |
35580 KB |
Output is correct |
18 |
Correct |
28 ms |
35584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
35532 KB |
Output is correct |
2 |
Correct |
29 ms |
35532 KB |
Output is correct |
3 |
Correct |
29 ms |
35580 KB |
Output is correct |
4 |
Correct |
29 ms |
35576 KB |
Output is correct |
5 |
Correct |
27 ms |
35508 KB |
Output is correct |
6 |
Correct |
30 ms |
35576 KB |
Output is correct |
7 |
Correct |
29 ms |
35532 KB |
Output is correct |
8 |
Correct |
26 ms |
35544 KB |
Output is correct |
9 |
Correct |
25 ms |
35584 KB |
Output is correct |
10 |
Correct |
24 ms |
35520 KB |
Output is correct |
11 |
Correct |
28 ms |
35488 KB |
Output is correct |
12 |
Correct |
24 ms |
35528 KB |
Output is correct |
13 |
Correct |
25 ms |
35532 KB |
Output is correct |
14 |
Correct |
25 ms |
35476 KB |
Output is correct |
15 |
Correct |
24 ms |
35580 KB |
Output is correct |
16 |
Correct |
24 ms |
35560 KB |
Output is correct |
17 |
Correct |
26 ms |
35580 KB |
Output is correct |
18 |
Correct |
28 ms |
35584 KB |
Output is correct |
19 |
Correct |
25 ms |
35532 KB |
Output is correct |
20 |
Correct |
25 ms |
35536 KB |
Output is correct |
21 |
Correct |
32 ms |
35904 KB |
Output is correct |
22 |
Correct |
26 ms |
36068 KB |
Output is correct |
23 |
Correct |
32 ms |
36044 KB |
Output is correct |
24 |
Correct |
31 ms |
36056 KB |
Output is correct |
25 |
Correct |
26 ms |
35964 KB |
Output is correct |
26 |
Correct |
32 ms |
36044 KB |
Output is correct |
27 |
Correct |
24 ms |
35700 KB |
Output is correct |
28 |
Correct |
27 ms |
35968 KB |
Output is correct |
29 |
Correct |
27 ms |
35968 KB |
Output is correct |
30 |
Correct |
27 ms |
36016 KB |
Output is correct |
31 |
Correct |
27 ms |
36044 KB |
Output is correct |
32 |
Correct |
26 ms |
35988 KB |
Output is correct |
33 |
Correct |
26 ms |
36056 KB |
Output is correct |
34 |
Correct |
32 ms |
35800 KB |
Output is correct |
35 |
Correct |
25 ms |
35848 KB |
Output is correct |
36 |
Correct |
27 ms |
35736 KB |
Output is correct |
37 |
Correct |
26 ms |
35748 KB |
Output is correct |
38 |
Correct |
26 ms |
35824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
35532 KB |
Output is correct |
2 |
Correct |
29 ms |
35532 KB |
Output is correct |
3 |
Correct |
29 ms |
35580 KB |
Output is correct |
4 |
Correct |
29 ms |
35576 KB |
Output is correct |
5 |
Correct |
27 ms |
35508 KB |
Output is correct |
6 |
Correct |
30 ms |
35576 KB |
Output is correct |
7 |
Correct |
29 ms |
35532 KB |
Output is correct |
8 |
Correct |
26 ms |
35544 KB |
Output is correct |
9 |
Correct |
25 ms |
35584 KB |
Output is correct |
10 |
Correct |
24 ms |
35520 KB |
Output is correct |
11 |
Correct |
28 ms |
35488 KB |
Output is correct |
12 |
Correct |
24 ms |
35528 KB |
Output is correct |
13 |
Correct |
25 ms |
35532 KB |
Output is correct |
14 |
Correct |
25 ms |
35476 KB |
Output is correct |
15 |
Correct |
24 ms |
35580 KB |
Output is correct |
16 |
Correct |
24 ms |
35560 KB |
Output is correct |
17 |
Correct |
26 ms |
35580 KB |
Output is correct |
18 |
Correct |
28 ms |
35584 KB |
Output is correct |
19 |
Correct |
238 ms |
69992 KB |
Output is correct |
20 |
Correct |
234 ms |
69936 KB |
Output is correct |
21 |
Correct |
231 ms |
70340 KB |
Output is correct |
22 |
Correct |
242 ms |
70852 KB |
Output is correct |
23 |
Correct |
331 ms |
97988 KB |
Output is correct |
24 |
Correct |
272 ms |
86252 KB |
Output is correct |
25 |
Correct |
133 ms |
56048 KB |
Output is correct |
26 |
Correct |
95 ms |
64980 KB |
Output is correct |
27 |
Correct |
371 ms |
88536 KB |
Output is correct |
28 |
Correct |
514 ms |
118516 KB |
Output is correct |
29 |
Correct |
526 ms |
117184 KB |
Output is correct |
30 |
Correct |
385 ms |
88520 KB |
Output is correct |
31 |
Correct |
362 ms |
88516 KB |
Output is correct |
32 |
Correct |
486 ms |
89768 KB |
Output is correct |
33 |
Correct |
307 ms |
71492 KB |
Output is correct |
34 |
Correct |
793 ms |
143468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
35532 KB |
Output is correct |
2 |
Correct |
29 ms |
35532 KB |
Output is correct |
3 |
Correct |
29 ms |
35580 KB |
Output is correct |
4 |
Correct |
29 ms |
35576 KB |
Output is correct |
5 |
Correct |
27 ms |
35508 KB |
Output is correct |
6 |
Correct |
30 ms |
35576 KB |
Output is correct |
7 |
Correct |
29 ms |
35532 KB |
Output is correct |
8 |
Correct |
26 ms |
35544 KB |
Output is correct |
9 |
Correct |
25 ms |
35584 KB |
Output is correct |
10 |
Correct |
24 ms |
35520 KB |
Output is correct |
11 |
Correct |
28 ms |
35488 KB |
Output is correct |
12 |
Correct |
24 ms |
35528 KB |
Output is correct |
13 |
Correct |
25 ms |
35532 KB |
Output is correct |
14 |
Correct |
25 ms |
35476 KB |
Output is correct |
15 |
Correct |
24 ms |
35580 KB |
Output is correct |
16 |
Correct |
24 ms |
35560 KB |
Output is correct |
17 |
Correct |
26 ms |
35580 KB |
Output is correct |
18 |
Correct |
28 ms |
35584 KB |
Output is correct |
19 |
Correct |
25 ms |
35532 KB |
Output is correct |
20 |
Correct |
25 ms |
35536 KB |
Output is correct |
21 |
Correct |
32 ms |
35904 KB |
Output is correct |
22 |
Correct |
26 ms |
36068 KB |
Output is correct |
23 |
Correct |
32 ms |
36044 KB |
Output is correct |
24 |
Correct |
31 ms |
36056 KB |
Output is correct |
25 |
Correct |
26 ms |
35964 KB |
Output is correct |
26 |
Correct |
32 ms |
36044 KB |
Output is correct |
27 |
Correct |
24 ms |
35700 KB |
Output is correct |
28 |
Correct |
27 ms |
35968 KB |
Output is correct |
29 |
Correct |
27 ms |
35968 KB |
Output is correct |
30 |
Correct |
27 ms |
36016 KB |
Output is correct |
31 |
Correct |
27 ms |
36044 KB |
Output is correct |
32 |
Correct |
26 ms |
35988 KB |
Output is correct |
33 |
Correct |
26 ms |
36056 KB |
Output is correct |
34 |
Correct |
32 ms |
35800 KB |
Output is correct |
35 |
Correct |
25 ms |
35848 KB |
Output is correct |
36 |
Correct |
27 ms |
35736 KB |
Output is correct |
37 |
Correct |
26 ms |
35748 KB |
Output is correct |
38 |
Correct |
26 ms |
35824 KB |
Output is correct |
39 |
Correct |
238 ms |
69992 KB |
Output is correct |
40 |
Correct |
234 ms |
69936 KB |
Output is correct |
41 |
Correct |
231 ms |
70340 KB |
Output is correct |
42 |
Correct |
242 ms |
70852 KB |
Output is correct |
43 |
Correct |
331 ms |
97988 KB |
Output is correct |
44 |
Correct |
272 ms |
86252 KB |
Output is correct |
45 |
Correct |
133 ms |
56048 KB |
Output is correct |
46 |
Correct |
95 ms |
64980 KB |
Output is correct |
47 |
Correct |
371 ms |
88536 KB |
Output is correct |
48 |
Correct |
514 ms |
118516 KB |
Output is correct |
49 |
Correct |
526 ms |
117184 KB |
Output is correct |
50 |
Correct |
385 ms |
88520 KB |
Output is correct |
51 |
Correct |
362 ms |
88516 KB |
Output is correct |
52 |
Correct |
486 ms |
89768 KB |
Output is correct |
53 |
Correct |
307 ms |
71492 KB |
Output is correct |
54 |
Correct |
793 ms |
143468 KB |
Output is correct |
55 |
Correct |
54 ms |
40896 KB |
Output is correct |
56 |
Correct |
41 ms |
38436 KB |
Output is correct |
57 |
Correct |
179 ms |
70148 KB |
Output is correct |
58 |
Correct |
121 ms |
66712 KB |
Output is correct |
59 |
Correct |
131 ms |
76884 KB |
Output is correct |
60 |
Correct |
447 ms |
117228 KB |
Output is correct |
61 |
Correct |
435 ms |
96280 KB |
Output is correct |
62 |
Correct |
369 ms |
89796 KB |
Output is correct |
63 |
Correct |
479 ms |
89784 KB |
Output is correct |
64 |
Correct |
974 ms |
189168 KB |
Output is correct |
65 |
Correct |
1090 ms |
190660 KB |
Output is correct |
66 |
Correct |
516 ms |
115164 KB |
Output is correct |
67 |
Correct |
350 ms |
98768 KB |
Output is correct |
68 |
Correct |
763 ms |
128132 KB |
Output is correct |
69 |
Correct |
787 ms |
129280 KB |
Output is correct |
70 |
Correct |
740 ms |
124164 KB |
Output is correct |