#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#define ll long long
#define endl "\n"
#define inf LLONG_MAX
#define vll vector<ll>
#define sd second
#define ft first
#define al(x) x.begin(),x.end()
#define alr(x) x.rbegin(),x.rend()
#define pll pair<ll, ll>
#define mod 1000000007
#define _set tree<pll, null_type, less<pll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
vector<vector<pair<ll, ll>>> adj;
vector<ll> v;
vector<bool>visited;
inline ll dfs(ll n, ll p){
v[n]=1;
for(auto& [y, w]: adj[n]){
if(y==p || visited[y]==1)continue;
v[n]+=dfs(y, n);
}
return v[n];
}
inline ll _find(ll n, ll n1, ll p){
for(auto& [y, w]: adj[n]){
if(y==p)continue;
if(v[y]>n1/2 && !visited[y])return _find(y, n1, n);
}
return n;
}
map<ll, ll> m;
vector<ll> v3;
vector<pair<ll, ll>> v2;
ll cnt=0, lim, cnt1=0, cnt3=0, res=LLONG_MAX;
inline void dfs1(ll n, ll p){
// cout<<n<<" "<<cnt3<<" "<<cnt<<endl;
if(cnt<=lim)v2.push_back({cnt, cnt3});
if(cnt<=lim && v3[lim-cnt]!=LLONG_MAX)res=min(res, cnt3+v3[lim-cnt]);
for(auto& [y, w]: adj[n]){
if(y==p || visited[y])continue;
cnt+=w;
cnt3++;
dfs1(y, n);
cnt-=w;
cnt3--;
}
}
ll cnt2=0;
inline void centroid(ll n){
dfs(n, n);
ll o=_find(n, v[n], n);
visited[o]=1;
v2.clear();
ll i=0;
//cout<<o<<endl;
for(auto& [y, w]: adj[o]){
if(visited[y])continue;
cnt3++;
cnt+=w;
dfs1(y, o);
cnt3--;
cnt-=w;
for(; i<v2.size(); i++){v3[v2[i].ft]=min(v3[v2[i].ft], v2[i].sd);if(v2[i].ft==lim)res=min(res, v2[i].sd);}
}
for(auto& [y1, w]: v2)v3[y1]=LLONG_MAX;
for(auto& [y, w]: adj[o]){
if(visited[y])continue;
centroid(y);
}
}
int best_path(int N, int K, int H[][2], int L[]){
ll a, b, c, d, e, f;
a=N;
b=K;
lim=b;
adj.clear();
v.clear();
v3.clear();
visited.clear();
adj.resize(a+1);
v.resize(a+1);
v3.assign(lim+1, LLONG_MAX);
visited.assign(a+1, 0);
for(int i=0; i<a-1; i++){
c=H[i][1];
d=H[i][0];
e=L[i];
adj[c].push_back({d, e});
adj[d].push_back({c, e});
}
centroid(1);
return (res==LLONG_MAX?-1:res);
}
Compilation message
race.cpp: In function 'void centroid(long long int)':
race.cpp:66:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(; i<v2.size(); i++){v3[v2[i].ft]=min(v3[v2[i].ft], v2[i].sd);if(v2[i].ft==lim)res=min(res, v2[i].sd);}
| ~^~~~~~~~~~
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:75:23: warning: unused variable 'f' [-Wunused-variable]
75 | ll a, b, c, d, e, f;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
428 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
428 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
7772 KB |
Output is correct |
23 |
Correct |
4 ms |
6328 KB |
Output is correct |
24 |
Correct |
3 ms |
7260 KB |
Output is correct |
25 |
Correct |
4 ms |
7004 KB |
Output is correct |
26 |
Correct |
2 ms |
3160 KB |
Output is correct |
27 |
Correct |
3 ms |
6748 KB |
Output is correct |
28 |
Correct |
1 ms |
2140 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
3420 KB |
Output is correct |
31 |
Correct |
3 ms |
5468 KB |
Output is correct |
32 |
Correct |
3 ms |
6012 KB |
Output is correct |
33 |
Correct |
4 ms |
6488 KB |
Output is correct |
34 |
Correct |
3 ms |
4956 KB |
Output is correct |
35 |
Correct |
3 ms |
6816 KB |
Output is correct |
36 |
Correct |
3 ms |
7820 KB |
Output is correct |
37 |
Correct |
3 ms |
6748 KB |
Output is correct |
38 |
Correct |
3 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
428 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
85 ms |
11600 KB |
Output is correct |
20 |
Correct |
79 ms |
11460 KB |
Output is correct |
21 |
Correct |
77 ms |
12240 KB |
Output is correct |
22 |
Correct |
72 ms |
13364 KB |
Output is correct |
23 |
Correct |
60 ms |
11600 KB |
Output is correct |
24 |
Correct |
47 ms |
10692 KB |
Output is correct |
25 |
Correct |
95 ms |
14732 KB |
Output is correct |
26 |
Correct |
60 ms |
16796 KB |
Output is correct |
27 |
Correct |
113 ms |
22868 KB |
Output is correct |
28 |
Correct |
203 ms |
29324 KB |
Output is correct |
29 |
Correct |
201 ms |
29196 KB |
Output is correct |
30 |
Correct |
110 ms |
22864 KB |
Output is correct |
31 |
Correct |
110 ms |
22868 KB |
Output is correct |
32 |
Correct |
125 ms |
22848 KB |
Output is correct |
33 |
Correct |
151 ms |
21332 KB |
Output is correct |
34 |
Correct |
151 ms |
22152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
428 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
7772 KB |
Output is correct |
23 |
Correct |
4 ms |
6328 KB |
Output is correct |
24 |
Correct |
3 ms |
7260 KB |
Output is correct |
25 |
Correct |
4 ms |
7004 KB |
Output is correct |
26 |
Correct |
2 ms |
3160 KB |
Output is correct |
27 |
Correct |
3 ms |
6748 KB |
Output is correct |
28 |
Correct |
1 ms |
2140 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
3420 KB |
Output is correct |
31 |
Correct |
3 ms |
5468 KB |
Output is correct |
32 |
Correct |
3 ms |
6012 KB |
Output is correct |
33 |
Correct |
4 ms |
6488 KB |
Output is correct |
34 |
Correct |
3 ms |
4956 KB |
Output is correct |
35 |
Correct |
3 ms |
6816 KB |
Output is correct |
36 |
Correct |
3 ms |
7820 KB |
Output is correct |
37 |
Correct |
3 ms |
6748 KB |
Output is correct |
38 |
Correct |
3 ms |
4444 KB |
Output is correct |
39 |
Correct |
85 ms |
11600 KB |
Output is correct |
40 |
Correct |
79 ms |
11460 KB |
Output is correct |
41 |
Correct |
77 ms |
12240 KB |
Output is correct |
42 |
Correct |
72 ms |
13364 KB |
Output is correct |
43 |
Correct |
60 ms |
11600 KB |
Output is correct |
44 |
Correct |
47 ms |
10692 KB |
Output is correct |
45 |
Correct |
95 ms |
14732 KB |
Output is correct |
46 |
Correct |
60 ms |
16796 KB |
Output is correct |
47 |
Correct |
113 ms |
22868 KB |
Output is correct |
48 |
Correct |
203 ms |
29324 KB |
Output is correct |
49 |
Correct |
201 ms |
29196 KB |
Output is correct |
50 |
Correct |
110 ms |
22864 KB |
Output is correct |
51 |
Correct |
110 ms |
22868 KB |
Output is correct |
52 |
Correct |
125 ms |
22848 KB |
Output is correct |
53 |
Correct |
151 ms |
21332 KB |
Output is correct |
54 |
Correct |
151 ms |
22152 KB |
Output is correct |
55 |
Correct |
6 ms |
1628 KB |
Output is correct |
56 |
Correct |
6 ms |
1880 KB |
Output is correct |
57 |
Correct |
46 ms |
13748 KB |
Output is correct |
58 |
Correct |
26 ms |
13516 KB |
Output is correct |
59 |
Correct |
60 ms |
18124 KB |
Output is correct |
60 |
Correct |
217 ms |
41184 KB |
Output is correct |
61 |
Correct |
100 ms |
24012 KB |
Output is correct |
62 |
Correct |
114 ms |
34784 KB |
Output is correct |
63 |
Correct |
129 ms |
34760 KB |
Output is correct |
64 |
Correct |
225 ms |
30952 KB |
Output is correct |
65 |
Correct |
146 ms |
23124 KB |
Output is correct |
66 |
Correct |
238 ms |
37452 KB |
Output is correct |
67 |
Correct |
68 ms |
35852 KB |
Output is correct |
68 |
Correct |
134 ms |
32716 KB |
Output is correct |
69 |
Correct |
137 ms |
32712 KB |
Output is correct |
70 |
Correct |
124 ms |
31552 KB |
Output is correct |