#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int dp[N];
int h[N];
bool viz[N];
vector<int> gr[N];
int par[N];
void first_dfs(int nod, int dad){
par[nod] = dad;
h[nod] = 1 + h[dad];
for(auto x:gr[nod]){
if(x != dad)
first_dfs(x, nod);
}
}
void dfs(int nod, int dad){
int nrvec = 0;
for(auto x:gr[nod]){
if(x == dad || viz[x] == true)
continue;
nrvec++;
dfs(x, nod);
}
if(nrvec == 1){
dp[nod] = 1;
return;
}
int m1 = 0, m2 = 0;
for(auto x:gr[nod]){
if(x == dad || viz[x])
continue;
if(nrvec + dp[x] > m1){
m2 = m1;
m1 = nrvec + dp[x];
}
else if(nrvec + dp[x] > m2){
m2 = nrvec + dp[x];
}
}
dp[nod] = m2;
}
vector<pair<int, int>> v;
int last;
int solve(int nod, int climbed){
viz[nod] = true;
if(par[nod] == 0)
return 0;
int sum = solve(par[nod], climbed + 1);
sum += gr[nod].size() - (par[nod] != 0) - (nod != last);
vector<int> vec;
for(auto x:gr[nod]){
if(viz[x] == false){
dfs(x, nod);
v.push_back({climbed + 1, dp[x] + sum});
}
}
return sum;
}
bool check(int t){
int lelm = -1, nrelm = 0;
int erased = 0;
for(auto x:v){
int auxt = t;
if(lelm == x.first){
auxt += nrelm;
}
if(x.second > auxt){
if(x.first <= erased)
return false;
if(t > 0){
t--;
erased++;
if(lelm != x.first){
lelm = x.first;
nrelm = 1;
}
else if(lelm == x.first)
nrelm++;
}
else
return false;
}
}
return true;
}
int main()
{
//freopen(".in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n, root, m;
cin>>n>>root>>m;
for(int i =1 ; i<n; i++){
int x, y;
cin>>x>>y;
gr[x].push_back(y);
gr[y].push_back(x);
}
first_dfs(root, 0);
last = m;
solve(m, 0);
sort(v.begin(), v.end());
int ans = 0;
for(int pas = 1<<30; pas > 0; pas /=2 ){
if(check(ans + pas) == false)
ans+=pas;
}
if(check(ans) == false)
ans++;
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23724 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
13 ms |
23756 KB |
Output is correct |
4 |
Correct |
15 ms |
23828 KB |
Output is correct |
5 |
Correct |
13 ms |
23756 KB |
Output is correct |
6 |
Correct |
13 ms |
23812 KB |
Output is correct |
7 |
Correct |
13 ms |
23820 KB |
Output is correct |
8 |
Correct |
13 ms |
23816 KB |
Output is correct |
9 |
Correct |
13 ms |
23816 KB |
Output is correct |
10 |
Correct |
15 ms |
23760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
354 ms |
66788 KB |
Output is correct |
2 |
Correct |
326 ms |
62924 KB |
Output is correct |
3 |
Correct |
970 ms |
68468 KB |
Output is correct |
4 |
Correct |
448 ms |
46360 KB |
Output is correct |
5 |
Correct |
940 ms |
68276 KB |
Output is correct |
6 |
Correct |
970 ms |
68236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23724 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
13 ms |
23756 KB |
Output is correct |
4 |
Correct |
15 ms |
23828 KB |
Output is correct |
5 |
Correct |
13 ms |
23756 KB |
Output is correct |
6 |
Correct |
13 ms |
23812 KB |
Output is correct |
7 |
Correct |
13 ms |
23820 KB |
Output is correct |
8 |
Correct |
13 ms |
23816 KB |
Output is correct |
9 |
Correct |
13 ms |
23816 KB |
Output is correct |
10 |
Correct |
15 ms |
23760 KB |
Output is correct |
11 |
Correct |
14 ms |
23820 KB |
Output is correct |
12 |
Correct |
14 ms |
23864 KB |
Output is correct |
13 |
Correct |
13 ms |
23848 KB |
Output is correct |
14 |
Correct |
15 ms |
23884 KB |
Output is correct |
15 |
Correct |
15 ms |
23952 KB |
Output is correct |
16 |
Correct |
15 ms |
23820 KB |
Output is correct |
17 |
Correct |
14 ms |
23756 KB |
Output is correct |
18 |
Correct |
14 ms |
23812 KB |
Output is correct |
19 |
Correct |
14 ms |
23884 KB |
Output is correct |
20 |
Correct |
14 ms |
23812 KB |
Output is correct |
21 |
Correct |
14 ms |
23884 KB |
Output is correct |
22 |
Correct |
14 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23724 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
13 ms |
23756 KB |
Output is correct |
4 |
Correct |
15 ms |
23828 KB |
Output is correct |
5 |
Correct |
13 ms |
23756 KB |
Output is correct |
6 |
Correct |
13 ms |
23812 KB |
Output is correct |
7 |
Correct |
13 ms |
23820 KB |
Output is correct |
8 |
Correct |
13 ms |
23816 KB |
Output is correct |
9 |
Correct |
13 ms |
23816 KB |
Output is correct |
10 |
Correct |
15 ms |
23760 KB |
Output is correct |
11 |
Correct |
354 ms |
66788 KB |
Output is correct |
12 |
Correct |
326 ms |
62924 KB |
Output is correct |
13 |
Correct |
970 ms |
68468 KB |
Output is correct |
14 |
Correct |
448 ms |
46360 KB |
Output is correct |
15 |
Correct |
940 ms |
68276 KB |
Output is correct |
16 |
Correct |
970 ms |
68236 KB |
Output is correct |
17 |
Correct |
14 ms |
23820 KB |
Output is correct |
18 |
Correct |
14 ms |
23864 KB |
Output is correct |
19 |
Correct |
13 ms |
23848 KB |
Output is correct |
20 |
Correct |
15 ms |
23884 KB |
Output is correct |
21 |
Correct |
15 ms |
23952 KB |
Output is correct |
22 |
Correct |
15 ms |
23820 KB |
Output is correct |
23 |
Correct |
14 ms |
23756 KB |
Output is correct |
24 |
Correct |
14 ms |
23812 KB |
Output is correct |
25 |
Correct |
14 ms |
23884 KB |
Output is correct |
26 |
Correct |
14 ms |
23812 KB |
Output is correct |
27 |
Correct |
14 ms |
23884 KB |
Output is correct |
28 |
Correct |
14 ms |
23756 KB |
Output is correct |
29 |
Correct |
14 ms |
23712 KB |
Output is correct |
30 |
Correct |
367 ms |
71212 KB |
Output is correct |
31 |
Correct |
352 ms |
71360 KB |
Output is correct |
32 |
Correct |
422 ms |
162332 KB |
Output is correct |
33 |
Correct |
416 ms |
149632 KB |
Output is correct |
34 |
Correct |
967 ms |
72460 KB |
Output is correct |
35 |
Correct |
953 ms |
72428 KB |
Output is correct |
36 |
Correct |
934 ms |
83612 KB |
Output is correct |
37 |
Correct |
937 ms |
83636 KB |
Output is correct |
38 |
Correct |
758 ms |
85388 KB |
Output is correct |
39 |
Correct |
721 ms |
85388 KB |
Output is correct |
40 |
Correct |
772 ms |
85248 KB |
Output is correct |