#include<bits/stdc++.h>
#include "race.h"
using namespace std;
#define ll long long
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)
#define MAX_N 500000
const int inf=1e9;
int nn;
vector<ll>P, Order, ofst2;
vector<ll>ofst;
vector<vector<ll>>A;
vector<map<ll, ll>>Has;
map<pair<ll, ll>, ll>mp;
void pre(){
P.clear(), Order.clear(), ofst.clear(), ofst2.clear(), A.clear(), Has.clear(), mp.clear();
P.resize(nn+1), ofst.resize(nn+1), ofst2.resize(nn+1), A.resize(nn+1), Has.resize(nn+1);
}
void dfs(int u){
for(int i=0;i<A[u].size();i++){
int v=A[u][i];
if(v!=P[u])
P[v]=u, dfs(v);
}
Order.push_back(u);
}
int best_path(int n, int k, int H[][2], int L[]){
nn=n;
pre();
ll ans=inf;
for(int i=0;i<n-1;i++){
int u=H[i][0], v=H[i][1];
A[u].push_back(v);
A[v].push_back(u);
mp[{u, v}]=mp[{v, u}]=L[i];
}
dfs(0);
for(int kk=0;kk<n;kk++){
int u=Order[kk], mx=-1;
for(int i=0;i<A[u].size();i++){
int v=A[u][i];
if(v==P[u])
continue;
if(mx==-1 || Has[v].size()>Has[mx].size())
mx=v;
}
if(mx!=-1)
ofst[u]=ofst[mx]+mp[{u, mx}], ofst2[u]=ofst2[mx]+1, swap(Has[u], Has[mx]);
for(int i=0;i<A[u].size();i++){
int v=A[u][i];
if(v==P[u] || v==mx)
continue;
vector<array<ll, 2>>New;
for(auto it:Has[v]){
ll len=it.FF+ofst[v]+mp[{u, v}], need=it.SS+ofst2[v]+1;
if(Has[u].count(k-len-ofst[u]))
ans=min(ans, need+Has[u][k-len-ofst[u]]+ofst2[u]);
New.push_back({len, need});
}
for(int i=0;i<New.size();i++){
int len=New[i][0], need=New[i][1];
if(!Has[u].count(len-ofst[u]))
Has[u][len-ofst[u]]=need-ofst2[u];
else
Has[u][len-ofst[u]]=min(Has[u][len-ofst[u]], need-ofst2[u]);
}
}
Has[u][-ofst[u]]=-ofst2[u];
if(Has[u].count(k-ofst[u])){
ans=min(ans, Has[u][k-ofst[u]]+ofst2[u]);
}
}
if(ans==inf)
ans=-1;
return ans;
}
Compilation message
race.cpp: In function 'void dfs(int)':
race.cpp:24:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<A[u].size();i++){
| ~^~~~~~~~~~~~
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:44:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i=0;i<A[u].size();i++){
| ~^~~~~~~~~~~~
race.cpp:53:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i=0;i<A[u].size();i++){
| ~^~~~~~~~~~~~
race.cpp:64:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i=0;i<New.size();i++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
360 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
360 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
21 |
Correct |
2 ms |
828 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
704 KB |
Output is correct |
24 |
Correct |
2 ms |
724 KB |
Output is correct |
25 |
Correct |
2 ms |
724 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
2 ms |
724 KB |
Output is correct |
30 |
Correct |
2 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
3 ms |
724 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
360 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
187 ms |
39060 KB |
Output is correct |
20 |
Correct |
196 ms |
38972 KB |
Output is correct |
21 |
Correct |
191 ms |
39064 KB |
Output is correct |
22 |
Correct |
258 ms |
39160 KB |
Output is correct |
23 |
Correct |
280 ms |
52288 KB |
Output is correct |
24 |
Correct |
231 ms |
45328 KB |
Output is correct |
25 |
Correct |
194 ms |
32440 KB |
Output is correct |
26 |
Correct |
82 ms |
35568 KB |
Output is correct |
27 |
Correct |
476 ms |
68248 KB |
Output is correct |
28 |
Correct |
539 ms |
82584 KB |
Output is correct |
29 |
Correct |
531 ms |
82484 KB |
Output is correct |
30 |
Correct |
442 ms |
68304 KB |
Output is correct |
31 |
Correct |
450 ms |
68192 KB |
Output is correct |
32 |
Correct |
593 ms |
68168 KB |
Output is correct |
33 |
Correct |
539 ms |
66808 KB |
Output is correct |
34 |
Correct |
601 ms |
98776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
360 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
21 |
Correct |
2 ms |
828 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
704 KB |
Output is correct |
24 |
Correct |
2 ms |
724 KB |
Output is correct |
25 |
Correct |
2 ms |
724 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
2 ms |
724 KB |
Output is correct |
30 |
Correct |
2 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
3 ms |
724 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
39 |
Correct |
187 ms |
39060 KB |
Output is correct |
40 |
Correct |
196 ms |
38972 KB |
Output is correct |
41 |
Correct |
191 ms |
39064 KB |
Output is correct |
42 |
Correct |
258 ms |
39160 KB |
Output is correct |
43 |
Correct |
280 ms |
52288 KB |
Output is correct |
44 |
Correct |
231 ms |
45328 KB |
Output is correct |
45 |
Correct |
194 ms |
32440 KB |
Output is correct |
46 |
Correct |
82 ms |
35568 KB |
Output is correct |
47 |
Correct |
476 ms |
68248 KB |
Output is correct |
48 |
Correct |
539 ms |
82584 KB |
Output is correct |
49 |
Correct |
531 ms |
82484 KB |
Output is correct |
50 |
Correct |
442 ms |
68304 KB |
Output is correct |
51 |
Correct |
450 ms |
68192 KB |
Output is correct |
52 |
Correct |
593 ms |
68168 KB |
Output is correct |
53 |
Correct |
539 ms |
66808 KB |
Output is correct |
54 |
Correct |
601 ms |
98776 KB |
Output is correct |
55 |
Correct |
20 ms |
5332 KB |
Output is correct |
56 |
Correct |
11 ms |
3924 KB |
Output is correct |
57 |
Correct |
144 ms |
37312 KB |
Output is correct |
58 |
Correct |
109 ms |
34508 KB |
Output is correct |
59 |
Correct |
112 ms |
41472 KB |
Output is correct |
60 |
Correct |
523 ms |
82412 KB |
Output is correct |
61 |
Correct |
444 ms |
71672 KB |
Output is correct |
62 |
Correct |
401 ms |
68136 KB |
Output is correct |
63 |
Correct |
582 ms |
68156 KB |
Output is correct |
64 |
Correct |
861 ms |
118700 KB |
Output is correct |
65 |
Correct |
864 ms |
118844 KB |
Output is correct |
66 |
Correct |
546 ms |
82508 KB |
Output is correct |
67 |
Correct |
514 ms |
68620 KB |
Output is correct |
68 |
Correct |
786 ms |
85756 KB |
Output is correct |
69 |
Correct |
763 ms |
86716 KB |
Output is correct |
70 |
Correct |
721 ms |
81996 KB |
Output is correct |