#include "race.h"
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<61);
const int inf=(1<<30);
const int nmax=1e6+50;
const int mod=1e9+7;
using namespace std;
int x,y,i,n,cent[nmax],sz[nmax],k,ans=1e9,rs[nmax];
vector<pair<int,int> >a[nmax];
vector<int>vc;
void dfs_sz(int u,int p)
{
sz[u]=1;
for(int i=0;i<a[u].size();i++)
{
int v=a[u][i].fr;
if(cent[v] || v==p)continue;
dfs_sz(v,u);
sz[u]+=sz[v];
}
}
int dfs_cent(int u,int p,int siz)
{
for(int i=0;i<a[u].size();i++)
{
int v=a[u][i].fr;
if(cent[v] || v==p)continue;
if(sz[v]>siz/2)return dfs_cent(v,u,siz);
}
return u;
}
void dfs(int x,int p,int d,int lvl,int kp)
{
if(d>k)return;
if(kp)
{
rs[d]=min(rs[d],lvl);
vc.pb(d);
}
else if(rs[k-d]!=1e9)ans=min(ans,lvl+rs[k-d]);
for(int i=0;i<a[x].size();i++)
{
int y=a[x][i].fr,v=a[x][i].sc;
if(y==p || cent[y])continue;
if(!lvl)
{
dfs(y,x,d+v,lvl+1,0);
dfs(y,x,d+v,lvl+1,1);
}
else dfs(y,x,d+v,lvl+1,kp);
}
}
void decompose(int root,int p=-1)
{
dfs_sz(root,p);
int c=dfs_cent(root,p,sz[root]);
cent[c]=1;
vc.clear();
dfs(c,p,0,0,1);
for(int i=0;i<vc.size();i++)rs[vc[i]]=1e9;
for(int i=0;i<a[c].size();i++)
{
int v=a[c][i].fr;
if(cent[v])continue;
decompose(v,c);
}
}
int best_path(int n,int K,int h[][2],int l[])
{
k=K;
for(i=0;i<n-1;i++)
{
x=h[i][0]+1,y=h[i][1]+1;
int lt=l[i];
a[x].pb(pair<int,int>(y,lt));
a[y].pb(pair<int,int>(x,lt));
}
for(i=0;i<=k;i++)rs[i]=1e9;
decompose(1);
if(ans==1e9)return -1;
else return ans;
}
Compilation message
race.cpp: In function 'void dfs_sz(int, int)':
race.cpp:24:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[u].size();i++)
~^~~~~~~~~~~~
race.cpp: In function 'int dfs_cent(int, int, int)':
race.cpp:34:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[u].size();i++)
~^~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int, int, int, int)':
race.cpp:51:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[x].size();i++)
~^~~~~~~~~~~~
race.cpp: In function 'void decompose(int, int)':
race.cpp:70:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<vc.size();i++)rs[vc[i]]=1e9;
~^~~~~~~~~~
race.cpp:71:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[c].size();i++)
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23928 KB |
Output is correct |
5 |
Correct |
21 ms |
23800 KB |
Output is correct |
6 |
Correct |
23 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
23900 KB |
Output is correct |
8 |
Correct |
21 ms |
23896 KB |
Output is correct |
9 |
Correct |
20 ms |
23800 KB |
Output is correct |
10 |
Correct |
21 ms |
23800 KB |
Output is correct |
11 |
Correct |
21 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
23 ms |
23800 KB |
Output is correct |
14 |
Correct |
21 ms |
23928 KB |
Output is correct |
15 |
Correct |
21 ms |
23800 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
25 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23928 KB |
Output is correct |
5 |
Correct |
21 ms |
23800 KB |
Output is correct |
6 |
Correct |
23 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
23900 KB |
Output is correct |
8 |
Correct |
21 ms |
23896 KB |
Output is correct |
9 |
Correct |
20 ms |
23800 KB |
Output is correct |
10 |
Correct |
21 ms |
23800 KB |
Output is correct |
11 |
Correct |
21 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
23 ms |
23800 KB |
Output is correct |
14 |
Correct |
21 ms |
23928 KB |
Output is correct |
15 |
Correct |
21 ms |
23800 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
25 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23928 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
24 ms |
23900 KB |
Output is correct |
22 |
Correct |
27 ms |
27512 KB |
Output is correct |
23 |
Correct |
27 ms |
26816 KB |
Output is correct |
24 |
Correct |
27 ms |
27228 KB |
Output is correct |
25 |
Correct |
27 ms |
27128 KB |
Output is correct |
26 |
Correct |
25 ms |
25208 KB |
Output is correct |
27 |
Correct |
26 ms |
27012 KB |
Output is correct |
28 |
Correct |
24 ms |
24696 KB |
Output is correct |
29 |
Correct |
25 ms |
25208 KB |
Output is correct |
30 |
Correct |
25 ms |
25336 KB |
Output is correct |
31 |
Correct |
26 ms |
26488 KB |
Output is correct |
32 |
Correct |
26 ms |
26744 KB |
Output is correct |
33 |
Correct |
26 ms |
27000 KB |
Output is correct |
34 |
Correct |
25 ms |
26232 KB |
Output is correct |
35 |
Correct |
28 ms |
27128 KB |
Output is correct |
36 |
Correct |
27 ms |
27512 KB |
Output is correct |
37 |
Correct |
26 ms |
27000 KB |
Output is correct |
38 |
Correct |
25 ms |
25848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23928 KB |
Output is correct |
5 |
Correct |
21 ms |
23800 KB |
Output is correct |
6 |
Correct |
23 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
23900 KB |
Output is correct |
8 |
Correct |
21 ms |
23896 KB |
Output is correct |
9 |
Correct |
20 ms |
23800 KB |
Output is correct |
10 |
Correct |
21 ms |
23800 KB |
Output is correct |
11 |
Correct |
21 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
23 ms |
23800 KB |
Output is correct |
14 |
Correct |
21 ms |
23928 KB |
Output is correct |
15 |
Correct |
21 ms |
23800 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
25 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23928 KB |
Output is correct |
19 |
Correct |
181 ms |
29816 KB |
Output is correct |
20 |
Correct |
165 ms |
29560 KB |
Output is correct |
21 |
Correct |
174 ms |
29916 KB |
Output is correct |
22 |
Correct |
163 ms |
30132 KB |
Output is correct |
23 |
Correct |
109 ms |
29844 KB |
Output is correct |
24 |
Correct |
80 ms |
29740 KB |
Output is correct |
25 |
Correct |
165 ms |
30840 KB |
Output is correct |
26 |
Correct |
109 ms |
32884 KB |
Output is correct |
27 |
Correct |
206 ms |
35504 KB |
Output is correct |
28 |
Correct |
427 ms |
40824 KB |
Output is correct |
29 |
Correct |
344 ms |
40412 KB |
Output is correct |
30 |
Correct |
254 ms |
35368 KB |
Output is correct |
31 |
Correct |
231 ms |
35480 KB |
Output is correct |
32 |
Correct |
243 ms |
35448 KB |
Output is correct |
33 |
Correct |
301 ms |
34168 KB |
Output is correct |
34 |
Correct |
273 ms |
34272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23928 KB |
Output is correct |
5 |
Correct |
21 ms |
23800 KB |
Output is correct |
6 |
Correct |
23 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
23900 KB |
Output is correct |
8 |
Correct |
21 ms |
23896 KB |
Output is correct |
9 |
Correct |
20 ms |
23800 KB |
Output is correct |
10 |
Correct |
21 ms |
23800 KB |
Output is correct |
11 |
Correct |
21 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
23 ms |
23800 KB |
Output is correct |
14 |
Correct |
21 ms |
23928 KB |
Output is correct |
15 |
Correct |
21 ms |
23800 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
25 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23928 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
24 ms |
23900 KB |
Output is correct |
22 |
Correct |
27 ms |
27512 KB |
Output is correct |
23 |
Correct |
27 ms |
26816 KB |
Output is correct |
24 |
Correct |
27 ms |
27228 KB |
Output is correct |
25 |
Correct |
27 ms |
27128 KB |
Output is correct |
26 |
Correct |
25 ms |
25208 KB |
Output is correct |
27 |
Correct |
26 ms |
27012 KB |
Output is correct |
28 |
Correct |
24 ms |
24696 KB |
Output is correct |
29 |
Correct |
25 ms |
25208 KB |
Output is correct |
30 |
Correct |
25 ms |
25336 KB |
Output is correct |
31 |
Correct |
26 ms |
26488 KB |
Output is correct |
32 |
Correct |
26 ms |
26744 KB |
Output is correct |
33 |
Correct |
26 ms |
27000 KB |
Output is correct |
34 |
Correct |
25 ms |
26232 KB |
Output is correct |
35 |
Correct |
28 ms |
27128 KB |
Output is correct |
36 |
Correct |
27 ms |
27512 KB |
Output is correct |
37 |
Correct |
26 ms |
27000 KB |
Output is correct |
38 |
Correct |
25 ms |
25848 KB |
Output is correct |
39 |
Correct |
181 ms |
29816 KB |
Output is correct |
40 |
Correct |
165 ms |
29560 KB |
Output is correct |
41 |
Correct |
174 ms |
29916 KB |
Output is correct |
42 |
Correct |
163 ms |
30132 KB |
Output is correct |
43 |
Correct |
109 ms |
29844 KB |
Output is correct |
44 |
Correct |
80 ms |
29740 KB |
Output is correct |
45 |
Correct |
165 ms |
30840 KB |
Output is correct |
46 |
Correct |
109 ms |
32884 KB |
Output is correct |
47 |
Correct |
206 ms |
35504 KB |
Output is correct |
48 |
Correct |
427 ms |
40824 KB |
Output is correct |
49 |
Correct |
344 ms |
40412 KB |
Output is correct |
50 |
Correct |
254 ms |
35368 KB |
Output is correct |
51 |
Correct |
231 ms |
35480 KB |
Output is correct |
52 |
Correct |
243 ms |
35448 KB |
Output is correct |
53 |
Correct |
301 ms |
34168 KB |
Output is correct |
54 |
Correct |
273 ms |
34272 KB |
Output is correct |
55 |
Correct |
30 ms |
24696 KB |
Output is correct |
56 |
Correct |
31 ms |
24568 KB |
Output is correct |
57 |
Correct |
95 ms |
30420 KB |
Output is correct |
58 |
Correct |
53 ms |
30696 KB |
Output is correct |
59 |
Correct |
114 ms |
33652 KB |
Output is correct |
60 |
Correct |
474 ms |
45388 KB |
Output is correct |
61 |
Correct |
260 ms |
35816 KB |
Output is correct |
62 |
Correct |
290 ms |
40276 KB |
Output is correct |
63 |
Correct |
285 ms |
40196 KB |
Output is correct |
64 |
Correct |
496 ms |
38120 KB |
Output is correct |
65 |
Correct |
213 ms |
35448 KB |
Output is correct |
66 |
Correct |
452 ms |
43256 KB |
Output is correct |
67 |
Correct |
160 ms |
41416 KB |
Output is correct |
68 |
Correct |
312 ms |
39824 KB |
Output is correct |
69 |
Correct |
330 ms |
39988 KB |
Output is correct |
70 |
Correct |
274 ms |
39540 KB |
Output is correct |