#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 cur = 0;
for(auto x:v){
if(x.second + cur > t){
if(x.first > cur)
return false;
cur++;
}
}
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;
}
Compilation message
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:73:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(".in","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
36 ms |
48196 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
36 ms |
48176 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
36 ms |
48196 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
36 ms |
48196 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |