#include "race.h"
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>>vct[500000];
pair<pair<long long,long long>,map<long long,long long>>pr[500000];
long long ans=LONG_LONG_MAX,k;
vector<int>viss(500000);
int cnt=0;
void dfs(int in){
cnt++;
assert(!viss[in]);
viss[in]=1;
if(vct[in].empty()){
pr[in].second[0]=0;
return;
}
for(auto &i:vct[in])dfs(i.first);
int f=0,g=0;
for(int i=0;i<vct[in].size();i++){
if(pr[vct[in][i].first].second.size()>f){
f=pr[vct[in][i].first].second.size();
g=i;
}
}
swap(pr[in],pr[vct[in][g].first]);
pr[in].first.first+=vct[in][g].second;
pr[in].first.second++;
if(!pr[in].second.count(-pr[in].first.first))pr[in].second[-pr[in].first.first]=-pr[in].first.second;
else pr[in].second[-pr[in].first.first]=min(pr[in].second[-pr[in].first.first],-pr[in].first.second);
if(pr[in].second.count(k-pr[in].first.first)){
ans=min(ans,(long long)pr[in].second[k-pr[in].first.first]+pr[in].first.second);
}
for(int i=0;i<vct[in].size();i++){
if(i==g)continue;
vector<pair<long long,long long>>V;
for(auto &w:pr[vct[in][i].first].second){
long long a=w.first+pr[vct[in][i].first].first.first+vct[in][i].second,b=w.second+pr[vct[in][i].first].first.second+1;
long long G=k-a-pr[in].first.first;
if(pr[in].second.count(G)){
ans=min(ans,(long long)pr[in].second[G]+pr[in].first.second+b);
}
a-=pr[in].first.first,b-=pr[in].first.second;
V.push_back({a,b});
}
for(auto &w:V){
if(!pr[in].second.count(w.first))pr[in].second[w.first]=w.second;
else pr[in].second[w.first]=min(pr[in].second[w.first],w.second);
}
}
}
int best_path(int n, int K, int h[][2], int l[]){
k=K;
vector<pair<int,int>>vec[n];
for(int i=0;i<n-1;i++){
vec[h[i][0]].push_back({h[i][1],l[i]});
vec[h[i][1]].push_back({h[i][0],l[i]});
}
deque<int>dq={0};
vector<bool>vis(n);
vis[0]=1;
int c=0;
while(dq.size()){
c++;
int a=dq.front();
dq.pop_front();
for(auto &i:vec[a]){
if(!vis[i.first]){
vis[i.first]=1;
vct[a].push_back(i);
dq.push_back(i.first);
}
}
}
assert(c==n);
dfs(0);
return (ans==LONG_LONG_MAX?-1:ans);
}
Compilation message
race.cpp: In function 'void dfs(int)':
race.cpp:19:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(int i=0;i<vct[in].size();i++){
| ~^~~~~~~~~~~~~~~
race.cpp:20:46: warning: comparison of integer expressions of different signedness: 'std::map<long long int, long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
20 | if(pr[vct[in][i].first].second.size()>f){
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
race.cpp:33:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i=0;i<vct[in].size();i++){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
45268 KB |
Output is correct |
2 |
Correct |
24 ms |
45292 KB |
Output is correct |
3 |
Correct |
21 ms |
45240 KB |
Output is correct |
4 |
Correct |
20 ms |
45344 KB |
Output is correct |
5 |
Correct |
21 ms |
45248 KB |
Output is correct |
6 |
Correct |
24 ms |
45364 KB |
Output is correct |
7 |
Correct |
22 ms |
45360 KB |
Output is correct |
8 |
Correct |
20 ms |
45268 KB |
Output is correct |
9 |
Correct |
22 ms |
45296 KB |
Output is correct |
10 |
Correct |
20 ms |
45336 KB |
Output is correct |
11 |
Correct |
22 ms |
45316 KB |
Output is correct |
12 |
Correct |
21 ms |
45268 KB |
Output is correct |
13 |
Correct |
21 ms |
45268 KB |
Output is correct |
14 |
Correct |
22 ms |
45280 KB |
Output is correct |
15 |
Correct |
20 ms |
45268 KB |
Output is correct |
16 |
Correct |
23 ms |
45260 KB |
Output is correct |
17 |
Correct |
20 ms |
45332 KB |
Output is correct |
18 |
Correct |
22 ms |
45328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
45268 KB |
Output is correct |
2 |
Correct |
24 ms |
45292 KB |
Output is correct |
3 |
Correct |
21 ms |
45240 KB |
Output is correct |
4 |
Correct |
20 ms |
45344 KB |
Output is correct |
5 |
Correct |
21 ms |
45248 KB |
Output is correct |
6 |
Correct |
24 ms |
45364 KB |
Output is correct |
7 |
Correct |
22 ms |
45360 KB |
Output is correct |
8 |
Correct |
20 ms |
45268 KB |
Output is correct |
9 |
Correct |
22 ms |
45296 KB |
Output is correct |
10 |
Correct |
20 ms |
45336 KB |
Output is correct |
11 |
Correct |
22 ms |
45316 KB |
Output is correct |
12 |
Correct |
21 ms |
45268 KB |
Output is correct |
13 |
Correct |
21 ms |
45268 KB |
Output is correct |
14 |
Correct |
22 ms |
45280 KB |
Output is correct |
15 |
Correct |
20 ms |
45268 KB |
Output is correct |
16 |
Correct |
23 ms |
45260 KB |
Output is correct |
17 |
Correct |
20 ms |
45332 KB |
Output is correct |
18 |
Correct |
22 ms |
45328 KB |
Output is correct |
19 |
Correct |
21 ms |
45236 KB |
Output is correct |
20 |
Correct |
20 ms |
45312 KB |
Output is correct |
21 |
Correct |
21 ms |
45516 KB |
Output is correct |
22 |
Correct |
23 ms |
45652 KB |
Output is correct |
23 |
Correct |
21 ms |
45524 KB |
Output is correct |
24 |
Correct |
23 ms |
45520 KB |
Output is correct |
25 |
Correct |
22 ms |
45584 KB |
Output is correct |
26 |
Correct |
26 ms |
45560 KB |
Output is correct |
27 |
Correct |
27 ms |
45444 KB |
Output is correct |
28 |
Correct |
23 ms |
45508 KB |
Output is correct |
29 |
Correct |
22 ms |
45548 KB |
Output is correct |
30 |
Correct |
22 ms |
45544 KB |
Output is correct |
31 |
Correct |
22 ms |
45608 KB |
Output is correct |
32 |
Correct |
23 ms |
45544 KB |
Output is correct |
33 |
Correct |
23 ms |
45560 KB |
Output is correct |
34 |
Correct |
22 ms |
45516 KB |
Output is correct |
35 |
Correct |
22 ms |
45464 KB |
Output is correct |
36 |
Correct |
22 ms |
45464 KB |
Output is correct |
37 |
Correct |
21 ms |
45536 KB |
Output is correct |
38 |
Correct |
21 ms |
45576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
45268 KB |
Output is correct |
2 |
Correct |
24 ms |
45292 KB |
Output is correct |
3 |
Correct |
21 ms |
45240 KB |
Output is correct |
4 |
Correct |
20 ms |
45344 KB |
Output is correct |
5 |
Correct |
21 ms |
45248 KB |
Output is correct |
6 |
Correct |
24 ms |
45364 KB |
Output is correct |
7 |
Correct |
22 ms |
45360 KB |
Output is correct |
8 |
Correct |
20 ms |
45268 KB |
Output is correct |
9 |
Correct |
22 ms |
45296 KB |
Output is correct |
10 |
Correct |
20 ms |
45336 KB |
Output is correct |
11 |
Correct |
22 ms |
45316 KB |
Output is correct |
12 |
Correct |
21 ms |
45268 KB |
Output is correct |
13 |
Correct |
21 ms |
45268 KB |
Output is correct |
14 |
Correct |
22 ms |
45280 KB |
Output is correct |
15 |
Correct |
20 ms |
45268 KB |
Output is correct |
16 |
Correct |
23 ms |
45260 KB |
Output is correct |
17 |
Correct |
20 ms |
45332 KB |
Output is correct |
18 |
Correct |
22 ms |
45328 KB |
Output is correct |
19 |
Correct |
142 ms |
66180 KB |
Output is correct |
20 |
Correct |
160 ms |
65968 KB |
Output is correct |
21 |
Correct |
123 ms |
65992 KB |
Output is correct |
22 |
Correct |
129 ms |
66120 KB |
Output is correct |
23 |
Correct |
180 ms |
78888 KB |
Output is correct |
24 |
Correct |
151 ms |
71500 KB |
Output is correct |
25 |
Correct |
113 ms |
66380 KB |
Output is correct |
26 |
Correct |
73 ms |
74344 KB |
Output is correct |
27 |
Correct |
205 ms |
77680 KB |
Output is correct |
28 |
Correct |
297 ms |
115348 KB |
Output is correct |
29 |
Correct |
306 ms |
113680 KB |
Output is correct |
30 |
Correct |
194 ms |
77704 KB |
Output is correct |
31 |
Correct |
212 ms |
77660 KB |
Output is correct |
32 |
Correct |
230 ms |
77664 KB |
Output is correct |
33 |
Correct |
245 ms |
78288 KB |
Output is correct |
34 |
Correct |
387 ms |
110308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
45268 KB |
Output is correct |
2 |
Correct |
24 ms |
45292 KB |
Output is correct |
3 |
Correct |
21 ms |
45240 KB |
Output is correct |
4 |
Correct |
20 ms |
45344 KB |
Output is correct |
5 |
Correct |
21 ms |
45248 KB |
Output is correct |
6 |
Correct |
24 ms |
45364 KB |
Output is correct |
7 |
Correct |
22 ms |
45360 KB |
Output is correct |
8 |
Correct |
20 ms |
45268 KB |
Output is correct |
9 |
Correct |
22 ms |
45296 KB |
Output is correct |
10 |
Correct |
20 ms |
45336 KB |
Output is correct |
11 |
Correct |
22 ms |
45316 KB |
Output is correct |
12 |
Correct |
21 ms |
45268 KB |
Output is correct |
13 |
Correct |
21 ms |
45268 KB |
Output is correct |
14 |
Correct |
22 ms |
45280 KB |
Output is correct |
15 |
Correct |
20 ms |
45268 KB |
Output is correct |
16 |
Correct |
23 ms |
45260 KB |
Output is correct |
17 |
Correct |
20 ms |
45332 KB |
Output is correct |
18 |
Correct |
22 ms |
45328 KB |
Output is correct |
19 |
Correct |
21 ms |
45236 KB |
Output is correct |
20 |
Correct |
20 ms |
45312 KB |
Output is correct |
21 |
Correct |
21 ms |
45516 KB |
Output is correct |
22 |
Correct |
23 ms |
45652 KB |
Output is correct |
23 |
Correct |
21 ms |
45524 KB |
Output is correct |
24 |
Correct |
23 ms |
45520 KB |
Output is correct |
25 |
Correct |
22 ms |
45584 KB |
Output is correct |
26 |
Correct |
26 ms |
45560 KB |
Output is correct |
27 |
Correct |
27 ms |
45444 KB |
Output is correct |
28 |
Correct |
23 ms |
45508 KB |
Output is correct |
29 |
Correct |
22 ms |
45548 KB |
Output is correct |
30 |
Correct |
22 ms |
45544 KB |
Output is correct |
31 |
Correct |
22 ms |
45608 KB |
Output is correct |
32 |
Correct |
23 ms |
45544 KB |
Output is correct |
33 |
Correct |
23 ms |
45560 KB |
Output is correct |
34 |
Correct |
22 ms |
45516 KB |
Output is correct |
35 |
Correct |
22 ms |
45464 KB |
Output is correct |
36 |
Correct |
22 ms |
45464 KB |
Output is correct |
37 |
Correct |
21 ms |
45536 KB |
Output is correct |
38 |
Correct |
21 ms |
45576 KB |
Output is correct |
39 |
Correct |
142 ms |
66180 KB |
Output is correct |
40 |
Correct |
160 ms |
65968 KB |
Output is correct |
41 |
Correct |
123 ms |
65992 KB |
Output is correct |
42 |
Correct |
129 ms |
66120 KB |
Output is correct |
43 |
Correct |
180 ms |
78888 KB |
Output is correct |
44 |
Correct |
151 ms |
71500 KB |
Output is correct |
45 |
Correct |
113 ms |
66380 KB |
Output is correct |
46 |
Correct |
73 ms |
74344 KB |
Output is correct |
47 |
Correct |
205 ms |
77680 KB |
Output is correct |
48 |
Correct |
297 ms |
115348 KB |
Output is correct |
49 |
Correct |
306 ms |
113680 KB |
Output is correct |
50 |
Correct |
194 ms |
77704 KB |
Output is correct |
51 |
Correct |
212 ms |
77660 KB |
Output is correct |
52 |
Correct |
230 ms |
77664 KB |
Output is correct |
53 |
Correct |
245 ms |
78288 KB |
Output is correct |
54 |
Correct |
387 ms |
110308 KB |
Output is correct |
55 |
Correct |
35 ms |
48460 KB |
Output is correct |
56 |
Correct |
28 ms |
47180 KB |
Output is correct |
57 |
Correct |
85 ms |
64024 KB |
Output is correct |
58 |
Correct |
77 ms |
60808 KB |
Output is correct |
59 |
Correct |
103 ms |
81424 KB |
Output is correct |
60 |
Correct |
305 ms |
116840 KB |
Output is correct |
61 |
Correct |
232 ms |
83596 KB |
Output is correct |
62 |
Correct |
190 ms |
80096 KB |
Output is correct |
63 |
Correct |
238 ms |
80140 KB |
Output is correct |
64 |
Correct |
440 ms |
130232 KB |
Output is correct |
65 |
Correct |
446 ms |
130800 KB |
Output is correct |
66 |
Correct |
319 ms |
112836 KB |
Output is correct |
67 |
Correct |
203 ms |
77960 KB |
Output is correct |
68 |
Correct |
361 ms |
98060 KB |
Output is correct |
69 |
Correct |
329 ms |
98732 KB |
Output is correct |
70 |
Correct |
333 ms |
95880 KB |
Output is correct |