#include "bits/stdc++.h"
using namespace std;
#define double long double
#define mp make_pair
#define fi first
#define se second
#define pb push_back
mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 2e5 + 10;
int sz[MAXN], maxsz[MAXN], n, k;
vector<pair<int,int> >adj[MAXN];
unordered_map<int,int> f[MAXN];
const int MAXK = 10000010;
int exist[MAXK], exist2[MAXK];
int ans = 1e9;
int whole_size;
int number_of_operations = 0;
int dfs0(int node,int prev){
sz[node]=1;
number_of_operations++;
for(pair<int,int>x:adj[node]){
if(x.se==-1)continue;
if(x.fi==prev)continue;
int d=dfs0(x.fi,node);
sz[node]+=d;
}
return sz[node];
}
int dfs(int node,int prev){
sz[node]=1;
maxsz[node]=0;
number_of_operations++;
for(pair<int,int> x:adj[node]){
if(x.se==-1) continue;
if(x.fi==prev) continue;
int d=dfs(x.fi,node);
sz[node]+=d;
maxsz[node]=max(maxsz[node],d);
}
maxsz[node]=max(maxsz[node],whole_size-sz[node]);
return sz[node];
}
queue<int>ntv;
vector<int>all;
void wheewhoowhee(int node,int prev){
all.pb(node);
for(pair<int,int>x:adj[node]){
if(x.se==-1)continue;
if(x.fi==prev)continue;
wheewhoowhee(x.fi,node);
}
}
int divide(){
all.clear();
wheewhoowhee(ntv.front(),-1);
for(int i:all){
sz[i]=0;
maxsz[i]=1e9;
number_of_operations++;
}
whole_size=dfs0(ntv.front(),-1);
dfs(ntv.front(),-1);
ntv.pop();
int centroid=0, e=1e9;
for(int i:all){
if(e>maxsz[i]){
e=maxsz[i];
centroid=i;
}
}
for(pair<int,int> x:adj[centroid]){
if(x.se==-1) continue;
ntv.push(x.fi);
}
return centroid;
}
queue<int>used, used2, used3;
void dfs2(int node,int prev,int cur,int dep){
// cout <<cur <<"\n";
if(cur>=1 && cur<MAXK && exist2[cur]==1e9){
// cout << "FUCK\n";
used.push(cur);
used3.push(cur);
}
if(cur>=1 && cur<MAXK){
exist2[cur]=min(exist2[cur],dep);
}
for(pair<int,int>x:adj[node]){
if(x.se==-1) continue;
if(x.fi==prev) continue;
dfs2(x.fi,node,cur+x.se,dep+1);
}
}
void conquer(int root){
while(used2.size()){
exist[used2.front()]=1e9;
used2.pop();
}
for(int i=0;i<adj[root].size();i++){
if(adj[root][i].se==-1) continue;
int sto=adj[root][i].se;
adj[root][i].se=-1;
adj[adj[root][i].fi][f[adj[root][i].fi][root]].se=-1;
dfs2(adj[root][i].fi,-1,sto,1);
while(used3.size()){
// cout << "E "<<i<<" "<<used3.front()<<"\n";
if(k==used3.front()){
ans=min(ans,exist2[used3.front()]);
}
if(k-used3.front()>=1 && k-used3.front()<MAXK && exist[k-used3.front()]!=1e9){
// cout << "F "<<exist[k-used3.front()] << " " << exist2[used3.front()] << "\n";
ans=min(ans,exist[k-used3.front()]+exist2[used3.front()]);
// cout << "G "<<ans << "\n";
}
used3.pop();
}
while(used.size()){
if(exist[used.front()]==1e9) used2.push(used.front());
exist[used.front()]=min(exist[used.front()],exist2[used.front()]);
// cout << "exist "<<used.front()<<" "<<exist[used.front()]<<"\n";
exist2[used.front()]=1e9;
used.pop();
}
}
}
signed best_path(signed N,signed K,signed h[][2],signed l[]){
n=N, k=K;
for(int i=1;i<MAXK;i++) exist[i]=1e9, exist2[i]=1e9;
for(int i=1;i<n;i++){
int u=h[i-1][0],v=h[i-1][1],w=l[i-1];
//cin >> u >> v >> w;
u++, v++;
int U=adj[u].size();
int V=adj[v].size();
f[u][v]=U;
f[v][u]=V;
adj[u].pb({v,w});
adj[v].pb({u,w});
}
ntv.push(1);
while(ntv.size()){
conquer(divide());
}
return (ans==1e9 ? -1 : ans);
}
Compilation message
race.cpp: In function 'void conquer(int)':
race.cpp:99: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]
99 | for(int i=0;i<adj[root].size();i++){
| ~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
94316 KB |
Output is correct |
2 |
Correct |
55 ms |
94316 KB |
Output is correct |
3 |
Correct |
56 ms |
94316 KB |
Output is correct |
4 |
Correct |
63 ms |
94316 KB |
Output is correct |
5 |
Correct |
54 ms |
94316 KB |
Output is correct |
6 |
Correct |
56 ms |
94316 KB |
Output is correct |
7 |
Correct |
54 ms |
94316 KB |
Output is correct |
8 |
Correct |
55 ms |
94316 KB |
Output is correct |
9 |
Correct |
54 ms |
94336 KB |
Output is correct |
10 |
Correct |
54 ms |
94316 KB |
Output is correct |
11 |
Correct |
55 ms |
94316 KB |
Output is correct |
12 |
Correct |
56 ms |
94316 KB |
Output is correct |
13 |
Correct |
54 ms |
94316 KB |
Output is correct |
14 |
Correct |
56 ms |
94316 KB |
Output is correct |
15 |
Correct |
56 ms |
94316 KB |
Output is correct |
16 |
Correct |
55 ms |
94316 KB |
Output is correct |
17 |
Correct |
62 ms |
94316 KB |
Output is correct |
18 |
Correct |
56 ms |
94316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
94316 KB |
Output is correct |
2 |
Correct |
55 ms |
94316 KB |
Output is correct |
3 |
Correct |
56 ms |
94316 KB |
Output is correct |
4 |
Correct |
63 ms |
94316 KB |
Output is correct |
5 |
Correct |
54 ms |
94316 KB |
Output is correct |
6 |
Correct |
56 ms |
94316 KB |
Output is correct |
7 |
Correct |
54 ms |
94316 KB |
Output is correct |
8 |
Correct |
55 ms |
94316 KB |
Output is correct |
9 |
Correct |
54 ms |
94336 KB |
Output is correct |
10 |
Correct |
54 ms |
94316 KB |
Output is correct |
11 |
Correct |
55 ms |
94316 KB |
Output is correct |
12 |
Correct |
56 ms |
94316 KB |
Output is correct |
13 |
Correct |
54 ms |
94316 KB |
Output is correct |
14 |
Correct |
56 ms |
94316 KB |
Output is correct |
15 |
Correct |
56 ms |
94316 KB |
Output is correct |
16 |
Correct |
55 ms |
94316 KB |
Output is correct |
17 |
Correct |
62 ms |
94316 KB |
Output is correct |
18 |
Correct |
56 ms |
94316 KB |
Output is correct |
19 |
Correct |
55 ms |
94260 KB |
Output is correct |
20 |
Correct |
55 ms |
94316 KB |
Output is correct |
21 |
Correct |
55 ms |
94444 KB |
Output is correct |
22 |
Correct |
57 ms |
94592 KB |
Output is correct |
23 |
Correct |
70 ms |
94444 KB |
Output is correct |
24 |
Correct |
57 ms |
94444 KB |
Output is correct |
25 |
Correct |
58 ms |
94444 KB |
Output is correct |
26 |
Correct |
58 ms |
94444 KB |
Output is correct |
27 |
Correct |
56 ms |
94572 KB |
Output is correct |
28 |
Correct |
57 ms |
94444 KB |
Output is correct |
29 |
Correct |
58 ms |
94444 KB |
Output is correct |
30 |
Correct |
56 ms |
94464 KB |
Output is correct |
31 |
Correct |
57 ms |
94596 KB |
Output is correct |
32 |
Correct |
57 ms |
94444 KB |
Output is correct |
33 |
Correct |
56 ms |
94444 KB |
Output is correct |
34 |
Correct |
58 ms |
94444 KB |
Output is correct |
35 |
Correct |
69 ms |
94444 KB |
Output is correct |
36 |
Correct |
62 ms |
94444 KB |
Output is correct |
37 |
Correct |
62 ms |
94444 KB |
Output is correct |
38 |
Correct |
57 ms |
94444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
94316 KB |
Output is correct |
2 |
Correct |
55 ms |
94316 KB |
Output is correct |
3 |
Correct |
56 ms |
94316 KB |
Output is correct |
4 |
Correct |
63 ms |
94316 KB |
Output is correct |
5 |
Correct |
54 ms |
94316 KB |
Output is correct |
6 |
Correct |
56 ms |
94316 KB |
Output is correct |
7 |
Correct |
54 ms |
94316 KB |
Output is correct |
8 |
Correct |
55 ms |
94316 KB |
Output is correct |
9 |
Correct |
54 ms |
94336 KB |
Output is correct |
10 |
Correct |
54 ms |
94316 KB |
Output is correct |
11 |
Correct |
55 ms |
94316 KB |
Output is correct |
12 |
Correct |
56 ms |
94316 KB |
Output is correct |
13 |
Correct |
54 ms |
94316 KB |
Output is correct |
14 |
Correct |
56 ms |
94316 KB |
Output is correct |
15 |
Correct |
56 ms |
94316 KB |
Output is correct |
16 |
Correct |
55 ms |
94316 KB |
Output is correct |
17 |
Correct |
62 ms |
94316 KB |
Output is correct |
18 |
Correct |
56 ms |
94316 KB |
Output is correct |
19 |
Correct |
506 ms |
110276 KB |
Output is correct |
20 |
Correct |
544 ms |
110308 KB |
Output is correct |
21 |
Correct |
530 ms |
110316 KB |
Output is correct |
22 |
Correct |
486 ms |
110436 KB |
Output is correct |
23 |
Correct |
427 ms |
110956 KB |
Output is correct |
24 |
Correct |
263 ms |
111016 KB |
Output is correct |
25 |
Correct |
503 ms |
113540 KB |
Output is correct |
26 |
Correct |
225 ms |
117096 KB |
Output is correct |
27 |
Correct |
748 ms |
126564 KB |
Output is correct |
28 |
Correct |
1256 ms |
140896 KB |
Output is correct |
29 |
Correct |
1291 ms |
140128 KB |
Output is correct |
30 |
Correct |
751 ms |
126436 KB |
Output is correct |
31 |
Correct |
748 ms |
126436 KB |
Output is correct |
32 |
Correct |
990 ms |
126436 KB |
Output is correct |
33 |
Correct |
959 ms |
124516 KB |
Output is correct |
34 |
Correct |
1071 ms |
124240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
94316 KB |
Output is correct |
2 |
Correct |
55 ms |
94316 KB |
Output is correct |
3 |
Correct |
56 ms |
94316 KB |
Output is correct |
4 |
Correct |
63 ms |
94316 KB |
Output is correct |
5 |
Correct |
54 ms |
94316 KB |
Output is correct |
6 |
Correct |
56 ms |
94316 KB |
Output is correct |
7 |
Correct |
54 ms |
94316 KB |
Output is correct |
8 |
Correct |
55 ms |
94316 KB |
Output is correct |
9 |
Correct |
54 ms |
94336 KB |
Output is correct |
10 |
Correct |
54 ms |
94316 KB |
Output is correct |
11 |
Correct |
55 ms |
94316 KB |
Output is correct |
12 |
Correct |
56 ms |
94316 KB |
Output is correct |
13 |
Correct |
54 ms |
94316 KB |
Output is correct |
14 |
Correct |
56 ms |
94316 KB |
Output is correct |
15 |
Correct |
56 ms |
94316 KB |
Output is correct |
16 |
Correct |
55 ms |
94316 KB |
Output is correct |
17 |
Correct |
62 ms |
94316 KB |
Output is correct |
18 |
Correct |
56 ms |
94316 KB |
Output is correct |
19 |
Correct |
55 ms |
94260 KB |
Output is correct |
20 |
Correct |
55 ms |
94316 KB |
Output is correct |
21 |
Correct |
55 ms |
94444 KB |
Output is correct |
22 |
Correct |
57 ms |
94592 KB |
Output is correct |
23 |
Correct |
70 ms |
94444 KB |
Output is correct |
24 |
Correct |
57 ms |
94444 KB |
Output is correct |
25 |
Correct |
58 ms |
94444 KB |
Output is correct |
26 |
Correct |
58 ms |
94444 KB |
Output is correct |
27 |
Correct |
56 ms |
94572 KB |
Output is correct |
28 |
Correct |
57 ms |
94444 KB |
Output is correct |
29 |
Correct |
58 ms |
94444 KB |
Output is correct |
30 |
Correct |
56 ms |
94464 KB |
Output is correct |
31 |
Correct |
57 ms |
94596 KB |
Output is correct |
32 |
Correct |
57 ms |
94444 KB |
Output is correct |
33 |
Correct |
56 ms |
94444 KB |
Output is correct |
34 |
Correct |
58 ms |
94444 KB |
Output is correct |
35 |
Correct |
69 ms |
94444 KB |
Output is correct |
36 |
Correct |
62 ms |
94444 KB |
Output is correct |
37 |
Correct |
62 ms |
94444 KB |
Output is correct |
38 |
Correct |
57 ms |
94444 KB |
Output is correct |
39 |
Correct |
506 ms |
110276 KB |
Output is correct |
40 |
Correct |
544 ms |
110308 KB |
Output is correct |
41 |
Correct |
530 ms |
110316 KB |
Output is correct |
42 |
Correct |
486 ms |
110436 KB |
Output is correct |
43 |
Correct |
427 ms |
110956 KB |
Output is correct |
44 |
Correct |
263 ms |
111016 KB |
Output is correct |
45 |
Correct |
503 ms |
113540 KB |
Output is correct |
46 |
Correct |
225 ms |
117096 KB |
Output is correct |
47 |
Correct |
748 ms |
126564 KB |
Output is correct |
48 |
Correct |
1256 ms |
140896 KB |
Output is correct |
49 |
Correct |
1291 ms |
140128 KB |
Output is correct |
50 |
Correct |
751 ms |
126436 KB |
Output is correct |
51 |
Correct |
748 ms |
126436 KB |
Output is correct |
52 |
Correct |
990 ms |
126436 KB |
Output is correct |
53 |
Correct |
959 ms |
124516 KB |
Output is correct |
54 |
Correct |
1071 ms |
124240 KB |
Output is correct |
55 |
Correct |
74 ms |
96108 KB |
Output is correct |
56 |
Correct |
77 ms |
96256 KB |
Output is correct |
57 |
Correct |
245 ms |
112364 KB |
Output is correct |
58 |
Correct |
138 ms |
112516 KB |
Output is correct |
59 |
Correct |
297 ms |
119016 KB |
Output is correct |
60 |
Correct |
1191 ms |
143072 KB |
Output is correct |
61 |
Correct |
750 ms |
129636 KB |
Output is correct |
62 |
Correct |
704 ms |
129380 KB |
Output is correct |
63 |
Correct |
1026 ms |
129508 KB |
Output is correct |
64 |
Correct |
1440 ms |
130268 KB |
Output is correct |
65 |
Correct |
1315 ms |
131044 KB |
Output is correct |
66 |
Correct |
1393 ms |
139616 KB |
Output is correct |
67 |
Correct |
425 ms |
132080 KB |
Output is correct |
68 |
Correct |
1002 ms |
130664 KB |
Output is correct |
69 |
Correct |
982 ms |
131176 KB |
Output is correct |
70 |
Correct |
922 ms |
129120 KB |
Output is correct |