#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using pii=array<int,2>;
using tii=array<int,3>;
const int N=300005;
int n,r,a,b,dep[N],p[N],q[N],L[N],pA[N],subA[N];
vector<int> dp[N];
vector<int> dp2[N];
map<int,int> dp3[N],dp4[N];
vector<int> g[N],c[N],dA[N];
void dfs0(int u){
L[dep[u]]=u;
if(dep[u]>=a){
pA[u]=L[dep[u]-a];
subA[u]=L[dep[u]-a+1];
dA[pA[u]].push_back(u);
}
int idx=0;
for(int v: g[u]) if(p[u]!=v){
dep[v]=dep[u]+1;
p[v]=u;
q[v]=++idx;
c[u].push_back(v);
dfs0(v);
}
}
int dfs1(int u,int d);
int dfs2(int u,int d);
int Get(int u,int k){
int &ret=dp[u][k];
if(ret) return ret;
for(int i=0;i<(int)dA[u].size();i++){
int v=dA[u][i];
if(q[subA[v]]==k) continue;
if(dp2[u][i]){
ret=max(ret,dp2[u][i]);
continue;
}
int val=dfs1(v,b),w=v,d=0,e=0;
while(d<b){
e=q[w];
w=p[w];
d++;
val=min(val,Get(w,e));
if(d<b) for(int i=0;i<(int)c[w].size();i++) if(i+1!=e) val=min(val,dfs1(c[w][i],b-d-1));
}
dp2[u][i]=val;
ret=max(ret,val);
}
if(!ret){
ret=u;
for(int i=0;i<(int)c[u].size();i++) if(i+1!=k) ret=min(ret,dfs2(c[u][i],b-1));
}
debug(u,k,ret);
return ret;
}
int dfs1(int u,int d){
int &ret=dp3[u][d];
if(ret) return ret;
ret=Get(u,0);
if(d>=1) for(int v: c[u]) ret=min(ret,dfs1(v,d-1));
debug(u,d,0,ret);
return ret;
}
int dfs2(int u,int d){
int &ret=dp4[u][d];
if(ret) return ret;
ret=u;
if(d>=1) for(int v: c[u]) ret=min(ret,dfs2(v,d-1));
debug(u,d,1,ret);
return ret;
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>r>>a>>b;
if(a<=b){
cout<<"1\n";
return 0;
}
for(int u,v,i=1;i<n;i++){
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs0(r);
for(int i=1;i<=n;i++){
dp[i].resize(c[i].size()+1);
dp2[i].resize(dA[i].size());
}
cout<<Get(r,0);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
63832 KB |
Output is correct |
2 |
Correct |
20 ms |
63832 KB |
Output is correct |
3 |
Correct |
21 ms |
63836 KB |
Output is correct |
4 |
Correct |
21 ms |
63836 KB |
Output is correct |
5 |
Correct |
20 ms |
63656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
951 ms |
284372 KB |
Output is correct |
2 |
Correct |
321 ms |
176460 KB |
Output is correct |
3 |
Runtime error |
1993 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
63828 KB |
Output is correct |
2 |
Correct |
24 ms |
63972 KB |
Output is correct |
3 |
Correct |
23 ms |
63740 KB |
Output is correct |
4 |
Correct |
24 ms |
63972 KB |
Output is correct |
5 |
Correct |
26 ms |
63832 KB |
Output is correct |
6 |
Correct |
23 ms |
63960 KB |
Output is correct |
7 |
Correct |
27 ms |
64004 KB |
Output is correct |
8 |
Correct |
23 ms |
63836 KB |
Output is correct |
9 |
Correct |
23 ms |
63836 KB |
Output is correct |
10 |
Correct |
23 ms |
64004 KB |
Output is correct |
11 |
Correct |
23 ms |
64092 KB |
Output is correct |
12 |
Correct |
23 ms |
63836 KB |
Output is correct |
13 |
Correct |
23 ms |
63740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
63828 KB |
Output is correct |
2 |
Correct |
24 ms |
63972 KB |
Output is correct |
3 |
Correct |
23 ms |
63740 KB |
Output is correct |
4 |
Correct |
24 ms |
63972 KB |
Output is correct |
5 |
Correct |
26 ms |
63832 KB |
Output is correct |
6 |
Correct |
23 ms |
63960 KB |
Output is correct |
7 |
Correct |
27 ms |
64004 KB |
Output is correct |
8 |
Correct |
23 ms |
63836 KB |
Output is correct |
9 |
Correct |
23 ms |
63836 KB |
Output is correct |
10 |
Correct |
23 ms |
64004 KB |
Output is correct |
11 |
Correct |
23 ms |
64092 KB |
Output is correct |
12 |
Correct |
23 ms |
63836 KB |
Output is correct |
13 |
Correct |
23 ms |
63740 KB |
Output is correct |
14 |
Correct |
28 ms |
64652 KB |
Output is correct |
15 |
Correct |
26 ms |
64604 KB |
Output is correct |
16 |
Correct |
24 ms |
64604 KB |
Output is correct |
17 |
Correct |
25 ms |
64672 KB |
Output is correct |
18 |
Correct |
24 ms |
64508 KB |
Output is correct |
19 |
Correct |
27 ms |
64456 KB |
Output is correct |
20 |
Correct |
30 ms |
65220 KB |
Output is correct |
21 |
Correct |
24 ms |
64092 KB |
Output is correct |
22 |
Correct |
23 ms |
63836 KB |
Output is correct |
23 |
Correct |
43 ms |
71148 KB |
Output is correct |
24 |
Correct |
28 ms |
65344 KB |
Output is correct |
25 |
Correct |
46 ms |
72020 KB |
Output is correct |
26 |
Correct |
27 ms |
64092 KB |
Output is correct |
27 |
Correct |
24 ms |
64196 KB |
Output is correct |
28 |
Correct |
24 ms |
64088 KB |
Output is correct |
29 |
Correct |
27 ms |
64088 KB |
Output is correct |
30 |
Correct |
63 ms |
64596 KB |
Output is correct |
31 |
Correct |
62 ms |
64636 KB |
Output is correct |
32 |
Correct |
62 ms |
64456 KB |
Output is correct |
33 |
Correct |
80 ms |
82736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
93520 KB |
Output is correct |
2 |
Correct |
133 ms |
93524 KB |
Output is correct |
3 |
Correct |
164 ms |
94544 KB |
Output is correct |
4 |
Correct |
127 ms |
94544 KB |
Output is correct |
5 |
Correct |
109 ms |
87628 KB |
Output is correct |
6 |
Correct |
122 ms |
87892 KB |
Output is correct |
7 |
Correct |
258 ms |
111900 KB |
Output is correct |
8 |
Correct |
72 ms |
77420 KB |
Output is correct |
9 |
Correct |
438 ms |
160080 KB |
Output is correct |
10 |
Correct |
188 ms |
102740 KB |
Output is correct |
11 |
Correct |
67 ms |
73244 KB |
Output is correct |
12 |
Correct |
74 ms |
77612 KB |
Output is correct |
13 |
Correct |
25 ms |
63836 KB |
Output is correct |
14 |
Correct |
26 ms |
63836 KB |
Output is correct |
15 |
Correct |
526 ms |
150924 KB |
Output is correct |
16 |
Execution timed out |
6101 ms |
81180 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
63828 KB |
Output is correct |
2 |
Correct |
24 ms |
63972 KB |
Output is correct |
3 |
Correct |
23 ms |
63740 KB |
Output is correct |
4 |
Correct |
24 ms |
63972 KB |
Output is correct |
5 |
Correct |
26 ms |
63832 KB |
Output is correct |
6 |
Correct |
23 ms |
63960 KB |
Output is correct |
7 |
Correct |
27 ms |
64004 KB |
Output is correct |
8 |
Correct |
23 ms |
63836 KB |
Output is correct |
9 |
Correct |
23 ms |
63836 KB |
Output is correct |
10 |
Correct |
23 ms |
64004 KB |
Output is correct |
11 |
Correct |
23 ms |
64092 KB |
Output is correct |
12 |
Correct |
23 ms |
63836 KB |
Output is correct |
13 |
Correct |
23 ms |
63740 KB |
Output is correct |
14 |
Correct |
28 ms |
64652 KB |
Output is correct |
15 |
Correct |
26 ms |
64604 KB |
Output is correct |
16 |
Correct |
24 ms |
64604 KB |
Output is correct |
17 |
Correct |
25 ms |
64672 KB |
Output is correct |
18 |
Correct |
24 ms |
64508 KB |
Output is correct |
19 |
Correct |
27 ms |
64456 KB |
Output is correct |
20 |
Correct |
30 ms |
65220 KB |
Output is correct |
21 |
Correct |
24 ms |
64092 KB |
Output is correct |
22 |
Correct |
23 ms |
63836 KB |
Output is correct |
23 |
Correct |
43 ms |
71148 KB |
Output is correct |
24 |
Correct |
28 ms |
65344 KB |
Output is correct |
25 |
Correct |
46 ms |
72020 KB |
Output is correct |
26 |
Correct |
27 ms |
64092 KB |
Output is correct |
27 |
Correct |
24 ms |
64196 KB |
Output is correct |
28 |
Correct |
24 ms |
64088 KB |
Output is correct |
29 |
Correct |
27 ms |
64088 KB |
Output is correct |
30 |
Correct |
63 ms |
64596 KB |
Output is correct |
31 |
Correct |
62 ms |
64636 KB |
Output is correct |
32 |
Correct |
62 ms |
64456 KB |
Output is correct |
33 |
Correct |
80 ms |
82736 KB |
Output is correct |
34 |
Correct |
126 ms |
93392 KB |
Output is correct |
35 |
Correct |
127 ms |
93524 KB |
Output is correct |
36 |
Correct |
140 ms |
94592 KB |
Output is correct |
37 |
Correct |
131 ms |
94544 KB |
Output is correct |
38 |
Correct |
114 ms |
87732 KB |
Output is correct |
39 |
Correct |
115 ms |
87888 KB |
Output is correct |
40 |
Correct |
198 ms |
111952 KB |
Output is correct |
41 |
Correct |
65 ms |
77324 KB |
Output is correct |
42 |
Correct |
23 ms |
63832 KB |
Output is correct |
43 |
Correct |
1746 ms |
419752 KB |
Output is correct |
44 |
Execution timed out |
6068 ms |
81212 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
63832 KB |
Output is correct |
2 |
Correct |
20 ms |
63832 KB |
Output is correct |
3 |
Correct |
21 ms |
63836 KB |
Output is correct |
4 |
Correct |
21 ms |
63836 KB |
Output is correct |
5 |
Correct |
20 ms |
63656 KB |
Output is correct |
6 |
Correct |
951 ms |
284372 KB |
Output is correct |
7 |
Correct |
321 ms |
176460 KB |
Output is correct |
8 |
Runtime error |
1993 ms |
1048576 KB |
Execution killed with signal 9 |
9 |
Halted |
0 ms |
0 KB |
- |