#include <bits/stdc++.h>
#include "race.h"
#define F first
#define S second
using namespace std;
const int Nm=200005;
const int Mm=1000006;
int n,k,ANS=1e9,Lvl[Nm],F[Mm],F1[Mm];
pair < int , int > val;
vector < pair < int , int > > v[Nm];
int Dfs (int x,int size,int &Center,int p) {
int tot=1;
for (int i=0; i<v[x].size(); i++)
if (Lvl[v[x][i].F]==-1 && v[x][i].F!=p) tot+=Dfs(v[x][i].F,size,Center,x);
if (Center==-1 && (2*tot>=size || p==-1)) Center=x;
return tot;
}
void Df(int x,int p,int type) {
if (val.F==k) ANS=min(ANS,val.S);
if (val.F>=k) return ;
if (type==1) F[val.F]=min(F[val.F],val.S),ANS=min(ANS,F[val.F]+F1[k-val.F]);
else
if (type==2) F1[val.F]=min(F1[val.F],F[val.F]),F[val.F]=1e9;
else F1[val.F]=1e9;
for (int i=0; i<v[x].size(); i++) {
if (Lvl[v[x][i].F]==-1 && v[x][i].F!=p) {
val.F+=v[x][i].S,val.S++;
Df(v[x][i].F,x,type);
val.F-=v[x][i].S,val.S--;
}
}
}
void Build(int x,int size,int depth) {
int Center=-1;
Dfs(x,size,Center,-1);
Lvl[Center]=depth;
for (int i=0; i<v[Center].size(); i++)
if (Lvl[v[Center][i].F]==-1) {
val.F=v[Center][i].S,val.S=1;
Df(v[Center][i].F,Center,1);
Df(v[Center][i].F,Center,2);
val.F=0,val.S=0;
}
Df(Center,-1,3);
for (int i=0; i<v[Center].size(); i++)
if (Lvl[v[Center][i].F]==-1) Build(v[Center][i].F,size/2,depth+1);
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N,k=K;
for (int i=0; i<n-1; i++) {
v[H[i][0]].push_back({H[i][1],L[i]});
v[H[i][1]].push_back({H[i][0],L[i]});
}
for (int i=0; i<n; i++)
Lvl[i]=-1;
for (int i=1; i<=k; i++)
F1[i]=1e9,F[i]=1e9;
Build(1,n,0);
if (ANS==1e9) ANS=-1;
return ANS;
}
Compilation message
race.cpp: In function 'int Dfs(int, int, int&, int)':
race.cpp:17:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[x].size(); i++)
~^~~~~~~~~~~~
race.cpp: In function 'void Df(int, int, int)':
race.cpp:32:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[x].size(); i++) {
~^~~~~~~~~~~~
race.cpp: In function 'void Build(int, int, int)':
race.cpp:46:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[Center].size(); i++)
~^~~~~~~~~~~~~~~~~
race.cpp:56:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[Center].size(); i++)
~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
4984 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
6 ms |
4984 KB |
Output is correct |
14 |
Correct |
7 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
5088 KB |
Output is correct |
16 |
Correct |
6 ms |
4984 KB |
Output is correct |
17 |
Correct |
6 ms |
5116 KB |
Output is correct |
18 |
Correct |
6 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
4984 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
6 ms |
4984 KB |
Output is correct |
14 |
Correct |
7 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
5088 KB |
Output is correct |
16 |
Correct |
6 ms |
4984 KB |
Output is correct |
17 |
Correct |
6 ms |
5116 KB |
Output is correct |
18 |
Correct |
6 ms |
5112 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
6 ms |
4984 KB |
Output is correct |
21 |
Correct |
8 ms |
5112 KB |
Output is correct |
22 |
Correct |
14 ms |
12280 KB |
Output is correct |
23 |
Correct |
15 ms |
11000 KB |
Output is correct |
24 |
Correct |
15 ms |
11896 KB |
Output is correct |
25 |
Correct |
13 ms |
11640 KB |
Output is correct |
26 |
Correct |
9 ms |
7800 KB |
Output is correct |
27 |
Correct |
13 ms |
11428 KB |
Output is correct |
28 |
Correct |
9 ms |
6600 KB |
Output is correct |
29 |
Correct |
10 ms |
7672 KB |
Output is correct |
30 |
Correct |
10 ms |
7928 KB |
Output is correct |
31 |
Correct |
14 ms |
10236 KB |
Output is correct |
32 |
Correct |
12 ms |
10616 KB |
Output is correct |
33 |
Correct |
13 ms |
11256 KB |
Output is correct |
34 |
Correct |
11 ms |
9764 KB |
Output is correct |
35 |
Correct |
13 ms |
11384 KB |
Output is correct |
36 |
Correct |
14 ms |
12408 KB |
Output is correct |
37 |
Correct |
13 ms |
11256 KB |
Output is correct |
38 |
Correct |
11 ms |
9132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
4984 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
6 ms |
4984 KB |
Output is correct |
14 |
Correct |
7 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
5088 KB |
Output is correct |
16 |
Correct |
6 ms |
4984 KB |
Output is correct |
17 |
Correct |
6 ms |
5116 KB |
Output is correct |
18 |
Correct |
6 ms |
5112 KB |
Output is correct |
19 |
Correct |
232 ms |
11676 KB |
Output is correct |
20 |
Correct |
225 ms |
11640 KB |
Output is correct |
21 |
Correct |
265 ms |
11640 KB |
Output is correct |
22 |
Correct |
211 ms |
11820 KB |
Output is correct |
23 |
Correct |
118 ms |
12044 KB |
Output is correct |
24 |
Correct |
85 ms |
11856 KB |
Output is correct |
25 |
Correct |
200 ms |
18192 KB |
Output is correct |
26 |
Correct |
148 ms |
18956 KB |
Output is correct |
27 |
Correct |
232 ms |
18628 KB |
Output is correct |
28 |
Correct |
339 ms |
32224 KB |
Output is correct |
29 |
Correct |
312 ms |
26908 KB |
Output is correct |
30 |
Correct |
230 ms |
18552 KB |
Output is correct |
31 |
Correct |
243 ms |
18552 KB |
Output is correct |
32 |
Correct |
295 ms |
18820 KB |
Output is correct |
33 |
Correct |
326 ms |
17528 KB |
Output is correct |
34 |
Correct |
262 ms |
18524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
4984 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
6 ms |
4984 KB |
Output is correct |
14 |
Correct |
7 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
5088 KB |
Output is correct |
16 |
Correct |
6 ms |
4984 KB |
Output is correct |
17 |
Correct |
6 ms |
5116 KB |
Output is correct |
18 |
Correct |
6 ms |
5112 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
6 ms |
4984 KB |
Output is correct |
21 |
Correct |
8 ms |
5112 KB |
Output is correct |
22 |
Correct |
14 ms |
12280 KB |
Output is correct |
23 |
Correct |
15 ms |
11000 KB |
Output is correct |
24 |
Correct |
15 ms |
11896 KB |
Output is correct |
25 |
Correct |
13 ms |
11640 KB |
Output is correct |
26 |
Correct |
9 ms |
7800 KB |
Output is correct |
27 |
Correct |
13 ms |
11428 KB |
Output is correct |
28 |
Correct |
9 ms |
6600 KB |
Output is correct |
29 |
Correct |
10 ms |
7672 KB |
Output is correct |
30 |
Correct |
10 ms |
7928 KB |
Output is correct |
31 |
Correct |
14 ms |
10236 KB |
Output is correct |
32 |
Correct |
12 ms |
10616 KB |
Output is correct |
33 |
Correct |
13 ms |
11256 KB |
Output is correct |
34 |
Correct |
11 ms |
9764 KB |
Output is correct |
35 |
Correct |
13 ms |
11384 KB |
Output is correct |
36 |
Correct |
14 ms |
12408 KB |
Output is correct |
37 |
Correct |
13 ms |
11256 KB |
Output is correct |
38 |
Correct |
11 ms |
9132 KB |
Output is correct |
39 |
Correct |
232 ms |
11676 KB |
Output is correct |
40 |
Correct |
225 ms |
11640 KB |
Output is correct |
41 |
Correct |
265 ms |
11640 KB |
Output is correct |
42 |
Correct |
211 ms |
11820 KB |
Output is correct |
43 |
Correct |
118 ms |
12044 KB |
Output is correct |
44 |
Correct |
85 ms |
11856 KB |
Output is correct |
45 |
Correct |
200 ms |
18192 KB |
Output is correct |
46 |
Correct |
148 ms |
18956 KB |
Output is correct |
47 |
Correct |
232 ms |
18628 KB |
Output is correct |
48 |
Correct |
339 ms |
32224 KB |
Output is correct |
49 |
Correct |
312 ms |
26908 KB |
Output is correct |
50 |
Correct |
230 ms |
18552 KB |
Output is correct |
51 |
Correct |
243 ms |
18552 KB |
Output is correct |
52 |
Correct |
295 ms |
18820 KB |
Output is correct |
53 |
Correct |
326 ms |
17528 KB |
Output is correct |
54 |
Correct |
262 ms |
18524 KB |
Output is correct |
55 |
Correct |
20 ms |
5752 KB |
Output is correct |
56 |
Correct |
23 ms |
5752 KB |
Output is correct |
57 |
Correct |
128 ms |
11908 KB |
Output is correct |
58 |
Correct |
50 ms |
11616 KB |
Output is correct |
59 |
Correct |
153 ms |
20416 KB |
Output is correct |
60 |
Correct |
603 ms |
33444 KB |
Output is correct |
61 |
Correct |
283 ms |
18856 KB |
Output is correct |
62 |
Correct |
390 ms |
26552 KB |
Output is correct |
63 |
Correct |
537 ms |
26616 KB |
Output is correct |
64 |
Correct |
959 ms |
23160 KB |
Output is correct |
65 |
Correct |
276 ms |
19704 KB |
Output is correct |
66 |
Correct |
610 ms |
38776 KB |
Output is correct |
67 |
Correct |
160 ms |
27324 KB |
Output is correct |
68 |
Correct |
558 ms |
27272 KB |
Output is correct |
69 |
Correct |
545 ms |
27556 KB |
Output is correct |
70 |
Correct |
532 ms |
26744 KB |
Output is correct |