This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long double ld;
typedef pair<int, pair<int, int>> ftype;
const int maxn = 1e6+5;
const int inf = 1e9+5;
int n, m, t;
int l, r, mid;
vector<int> g[maxn];
int dp[maxn], onp[maxn];
void onp_dfs(int x, int p){
if(x == t){
onp[x] = 1;
return;
}
for(auto v: g[x]){
if(v == p)continue;
onp_dfs(v, x);
if(onp[v]){
dp[x]--;
onp[x] = 1;
}
}
//cout << x << " " << dp[x] << endl;
}
void dfs(int x, int p){
if(x == t)return;
dp[x] += g[x].size()-(p!=0);
for(auto v: g[x]){
if(v == p)continue;
dp[v] += dp[x];
dfs(v, x);
}
}
int dfs2(int x, int p, int tot = 0){
if(x == t)return 0;
for(auto v: g[x]){
if(v == p)continue;
if(onp[v])tot+=dfs2(v, x, 0);
}
dp[x] += tot;
for(auto v: g[x]){
if(v == p)continue;
if(!onp[v])dfs2(v, x, tot);
}
if(onp[x])tot+=g[x].size()-(p!=0)-1;
// cout << x << " " << dp[x] << " " << tot << endl;
return tot;
}
int chk(int x, int p){
if(dp[x] > mid)return inf;
if(x == t)return 0;
int re = inf, base = -inf;
vector<int> val;
for(auto v: g[x]){
if(v == p)continue;
int tmp = chk(v, x);
//cout << v << " " << tmp << endl;
if(onp[v])base = tmp;
else val.push_back(tmp);
}
sort(val.begin(), val.end());
re = min(re, int(val.size()));
if(!val.empty())re = min(re, max(val.back(), base));
else re = min(re, base);
for(int i = 0; i < val.size(); i++){
re = min(re, max(base, val[i]) + int(val.size())-i-1);
}
re--;
re = max(re, 0);
return re;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
//freopen("mousetrap.inp", "r", stdin);
// freopen("mousetrap.out", "w", stdout);
cin >> n >> t >> m;
for(int i = 0; i < n-1; i++){
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
onp_dfs(m, 0);
dfs(m, 0);
dfs2(m, 0);
int l = 0, r = maxn;
while(l < r){
mid = (l+r)>>1;
if(!chk(m, 0))r = mid;
else l = mid+1;
}
cout << l;
}
Compilation message (stderr)
mousetrap.cpp: In function 'int chk(int, int)':
mousetrap.cpp:78:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i = 0; i < val.size(); i++){
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |