#include <bits/stdc++.h>
using namespace std;
vector<int> graph[300009];
int par[300009], sp[20][300009], anc[300009], depth[300009], height[300009], mnv[300009]; //anc[v]: a-th ancestor of v
vector<int> child[300009];
int sz0[300009], sz[300009], mn[300009]; //sz[v]: number of a-th decendants of v, mn[v]: minimum value among self and 1~b-th dec, infinity if height >= a
bool cha[300009], chb0[300009], chb1[300009];
bool tch[300009];
queue<int> q; //impossible b positions
void settree(int v){
height[v] = 0;
for(int u: graph[v]){
if(par[v] == u)
continue;
par[u] = v;
child[v].push_back(u);
depth[u] = depth[v] + 1;
settree(u);
height[v] = max(height[v], height[u] + 1);
}
}
int getmin(int v, int d){
if(d==0)
return v;
int ret = v;
for(int u: child[v]){
ret = min(ret, getmin(u, d-1));
}
return ret;
}
void updatecircle(int v, int d){
tch[v] = 1;
if(!cha[v]){
cha[v] = 1;
sz[anc[v]]--;
if(!sz[anc[v]] && !chb1[anc[v]]){
chb1[anc[v]] = 1;
q.push(anc[v]);
}
}
if(d==0)
return;
for(int u: graph[v]){
if(!tch[u])
updatecircle(u, d-1);
}
}
void updatedescendant(int v, int d){
tch[v] = 1;
if(!cha[v]){
cha[v] = 1;
sz[anc[v]]--;
//printf("v=%d sz[%d]=%d\n", v, anc[v], sz[anc[v]]);
if(!sz[anc[v]] && !chb1[anc[v]]){
chb1[anc[v]] = 1;
q.push(anc[v]);
}
}
if(d==0)
return;
for(int u: child[v]){
if(!tch[u])
updatedescendant(u, d-1);
}
}
int main(){
int n, r, a, b, i, j, u, v;
scanf("%d %d %d %d", &n, &r, &a, &b);
assert(1 <= r && r <= n && 1<=a && a<=n && 1<=b && b<=n);
for(i=1; i<n; i++){
scanf("%d %d", &u, &v);
graph[u].push_back(v);
graph[v].push_back(u);
}
settree(r);
for(i=1; i<=n; i++){
if(height[i] >= a)
mn[i] = i;
else
mn[i] = getmin(i, b);
}
for(i=1; i<=n; i++)
sp[0][i] = par[i];
for(i=1; i<20; i++){
for(j=1; j<=n; j++){
sp[i][j] = sp[i-1][sp[i-1][j]];
}
}
for(i=1; i<=n; i++){
int cur = a;
int v = i;
for(j=19; j>=0; j--){
if(cur >= (1<<j)){
cur -= (1<<j);
v = sp[j][v];
}
}
anc[i] = v;
//printf("i=%d anc=%d\n", i, v);
sz0[v]++;
}
int lo = 1, hi = n, mid;
while(lo < hi){
mid = (lo+hi)/2;
memset(cha, 0 ,sizeof(cha));
for(i=1; i<=n; i++){
sz[i] = sz0[i];
chb0[i] = chb1[i] = 0;
if(mn[i] <= mid){
if(height[i] < a)
chb1[i] = 1;
else
chb0[i] = 1;
q.push(i);
}
}
//printf("mid=%d\n", mid);
while(!q.empty()){
int v = q.front();
//printf("v=%d\n", v);
q.pop();
memset(tch, 0, sizeof(tch));
if(chb1[v])
updatecircle(v, b);
else
updatedescendant(v, b);
}
if(chb1[r])
hi = mid;
else
lo = mid+1;
}
printf("%d\n", lo);
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:68:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%d %d %d %d", &n, &r, &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:71:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf("%d %d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6069 ms |
65772 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6018 ms |
101032 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
48472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
48472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2806 ms |
54828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
48472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6069 ms |
65772 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |