#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));
for(int i = 0; i < val.size(); i++){
if(val[i] >= re)re = min(re, 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 = 2*n;
while(l < r){
mid = (l+r)>>1;
if(!chk(m, 0))r = mid;
else l = mid+1;
}
cout << l;
}
Compilation message
mousetrap.cpp: In function 'int chk(int, int)':
mousetrap.cpp:77:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | 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 |
23816 KB |
Output is correct |
3 |
Correct |
13 ms |
23816 KB |
Output is correct |
4 |
Correct |
15 ms |
23784 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Incorrect |
13 ms |
23832 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
978 ms |
72428 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 |
23816 KB |
Output is correct |
3 |
Correct |
13 ms |
23816 KB |
Output is correct |
4 |
Correct |
15 ms |
23784 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Incorrect |
13 ms |
23832 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23816 KB |
Output is correct |
3 |
Correct |
13 ms |
23816 KB |
Output is correct |
4 |
Correct |
15 ms |
23784 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Incorrect |
13 ms |
23832 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |