#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--;
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) <= 0)r = mid;
else l = mid+1;
}
cout << l;
}
Compilation message
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 |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23788 KB |
Output is correct |
4 |
Correct |
13 ms |
23816 KB |
Output is correct |
5 |
Correct |
13 ms |
23784 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Incorrect |
13 ms |
23760 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
894 ms |
59028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23788 KB |
Output is correct |
4 |
Correct |
13 ms |
23816 KB |
Output is correct |
5 |
Correct |
13 ms |
23784 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Incorrect |
13 ms |
23760 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23788 KB |
Output is correct |
4 |
Correct |
13 ms |
23816 KB |
Output is correct |
5 |
Correct |
13 ms |
23784 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Incorrect |
13 ms |
23760 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |