#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],dp2[N];
int dp3[N][15],dp4[N][15];
vector<int> g[N],c[N],dA[N],idxsubA[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];
idxsubA[subA[u]].push_back(dA[pA[u]].size());
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);
struct ErasableSet{
priority_queue<int> pq1,pq2;
void insert(int x){
pq1.push(x);
}
void pop(int x){
pq2.push(x);
}
int top(){
while(pq2.size()&&pq1.top()==pq2.top()){
pq1.pop();
pq2.pop();
}
return pq1.top();
}
int size(){
return pq1.size()-pq2.size();
}
}dp2C[N];
int Get(int u,int k){
int &ret=dp[u][k];
if(ret) return ret;
if((int)dp2C[u].size()!=(int)dA[u].size()){
for(int i=0;i<(int)dA[u].size();i++){
int v=dA[u][i];
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;
dp2C[u].insert(dp2[u][i]);
}
}
if(dA[u].empty()||(k&&dA[u].size()==idxsubA[c[u][k-1]].size())){
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));
} else{
if(k){
for(int i: idxsubA[c[u][k-1]]){
dp2C[u].pop(dp2[u][i]);
}
}
ret=dp2C[u].top();
if(k){
for(int i: idxsubA[c[u][k-1]]) dp2C[u].insert(dp2[u][i]);
}
}
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
61528 KB |
Output is correct |
2 |
Correct |
23 ms |
61532 KB |
Output is correct |
3 |
Correct |
23 ms |
61304 KB |
Output is correct |
4 |
Correct |
23 ms |
61532 KB |
Output is correct |
5 |
Correct |
22 ms |
61496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
586 ms |
183224 KB |
Output is correct |
2 |
Correct |
418 ms |
182176 KB |
Output is correct |
3 |
Correct |
675 ms |
181848 KB |
Output is correct |
4 |
Execution timed out |
6014 ms |
158628 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
61532 KB |
Output is correct |
2 |
Correct |
23 ms |
61632 KB |
Output is correct |
3 |
Correct |
23 ms |
61576 KB |
Output is correct |
4 |
Correct |
24 ms |
61532 KB |
Output is correct |
5 |
Correct |
22 ms |
61524 KB |
Output is correct |
6 |
Correct |
22 ms |
61492 KB |
Output is correct |
7 |
Correct |
23 ms |
61528 KB |
Output is correct |
8 |
Correct |
22 ms |
61532 KB |
Output is correct |
9 |
Correct |
23 ms |
61272 KB |
Output is correct |
10 |
Correct |
23 ms |
61532 KB |
Output is correct |
11 |
Correct |
28 ms |
61532 KB |
Output is correct |
12 |
Correct |
30 ms |
61360 KB |
Output is correct |
13 |
Correct |
23 ms |
61532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
61532 KB |
Output is correct |
2 |
Correct |
23 ms |
61632 KB |
Output is correct |
3 |
Correct |
23 ms |
61576 KB |
Output is correct |
4 |
Correct |
24 ms |
61532 KB |
Output is correct |
5 |
Correct |
22 ms |
61524 KB |
Output is correct |
6 |
Correct |
22 ms |
61492 KB |
Output is correct |
7 |
Correct |
23 ms |
61528 KB |
Output is correct |
8 |
Correct |
22 ms |
61532 KB |
Output is correct |
9 |
Correct |
23 ms |
61272 KB |
Output is correct |
10 |
Correct |
23 ms |
61532 KB |
Output is correct |
11 |
Correct |
28 ms |
61532 KB |
Output is correct |
12 |
Correct |
30 ms |
61360 KB |
Output is correct |
13 |
Correct |
23 ms |
61532 KB |
Output is correct |
14 |
Correct |
26 ms |
62300 KB |
Output is correct |
15 |
Correct |
28 ms |
62296 KB |
Output is correct |
16 |
Correct |
24 ms |
62296 KB |
Output is correct |
17 |
Correct |
28 ms |
62300 KB |
Output is correct |
18 |
Correct |
26 ms |
62344 KB |
Output is correct |
19 |
Correct |
27 ms |
62288 KB |
Output is correct |
20 |
Correct |
28 ms |
62300 KB |
Output is correct |
21 |
Correct |
23 ms |
61788 KB |
Output is correct |
22 |
Correct |
22 ms |
61544 KB |
Output is correct |
23 |
Correct |
34 ms |
62044 KB |
Output is correct |
24 |
Correct |
25 ms |
62288 KB |
Output is correct |
25 |
Incorrect |
26 ms |
62300 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
88412 KB |
Output is correct |
2 |
Correct |
110 ms |
89684 KB |
Output is correct |
3 |
Correct |
99 ms |
88192 KB |
Output is correct |
4 |
Correct |
129 ms |
88912 KB |
Output is correct |
5 |
Correct |
98 ms |
88592 KB |
Output is correct |
6 |
Correct |
103 ms |
89748 KB |
Output is correct |
7 |
Correct |
108 ms |
87376 KB |
Output is correct |
8 |
Correct |
56 ms |
77508 KB |
Output is correct |
9 |
Correct |
132 ms |
86608 KB |
Output is correct |
10 |
Correct |
81 ms |
84364 KB |
Output is correct |
11 |
Correct |
54 ms |
70996 KB |
Output is correct |
12 |
Correct |
55 ms |
76364 KB |
Output is correct |
13 |
Correct |
23 ms |
61532 KB |
Output is correct |
14 |
Correct |
22 ms |
61272 KB |
Output is correct |
15 |
Correct |
142 ms |
82616 KB |
Output is correct |
16 |
Execution timed out |
6065 ms |
84040 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
61532 KB |
Output is correct |
2 |
Correct |
23 ms |
61632 KB |
Output is correct |
3 |
Correct |
23 ms |
61576 KB |
Output is correct |
4 |
Correct |
24 ms |
61532 KB |
Output is correct |
5 |
Correct |
22 ms |
61524 KB |
Output is correct |
6 |
Correct |
22 ms |
61492 KB |
Output is correct |
7 |
Correct |
23 ms |
61528 KB |
Output is correct |
8 |
Correct |
22 ms |
61532 KB |
Output is correct |
9 |
Correct |
23 ms |
61272 KB |
Output is correct |
10 |
Correct |
23 ms |
61532 KB |
Output is correct |
11 |
Correct |
28 ms |
61532 KB |
Output is correct |
12 |
Correct |
30 ms |
61360 KB |
Output is correct |
13 |
Correct |
23 ms |
61532 KB |
Output is correct |
14 |
Correct |
26 ms |
62300 KB |
Output is correct |
15 |
Correct |
28 ms |
62296 KB |
Output is correct |
16 |
Correct |
24 ms |
62296 KB |
Output is correct |
17 |
Correct |
28 ms |
62300 KB |
Output is correct |
18 |
Correct |
26 ms |
62344 KB |
Output is correct |
19 |
Correct |
27 ms |
62288 KB |
Output is correct |
20 |
Correct |
28 ms |
62300 KB |
Output is correct |
21 |
Correct |
23 ms |
61788 KB |
Output is correct |
22 |
Correct |
22 ms |
61544 KB |
Output is correct |
23 |
Correct |
34 ms |
62044 KB |
Output is correct |
24 |
Correct |
25 ms |
62288 KB |
Output is correct |
25 |
Incorrect |
26 ms |
62300 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
61528 KB |
Output is correct |
2 |
Correct |
23 ms |
61532 KB |
Output is correct |
3 |
Correct |
23 ms |
61304 KB |
Output is correct |
4 |
Correct |
23 ms |
61532 KB |
Output is correct |
5 |
Correct |
22 ms |
61496 KB |
Output is correct |
6 |
Correct |
586 ms |
183224 KB |
Output is correct |
7 |
Correct |
418 ms |
182176 KB |
Output is correct |
8 |
Correct |
675 ms |
181848 KB |
Output is correct |
9 |
Execution timed out |
6014 ms |
158628 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |