#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef string str;
const ll Mod = 1e9 + 7;
const int Maxn = 1e6 + 100;
vector<ll> G[Maxn];
ll dp[Maxn], son[Maxn], m1[Maxn], m2[Maxn];
ll t, m;
vector<ll> V;
vector<ll> S[Maxn];
bool DFS(ll u, ll p){
bool res = (u == m), r2;
ll cnt = 0;
ll mx = 0;
ll mx2 = 0;
for(auto adj : G[u]){
if(adj == p) continue;
r2 =DFS(adj, u);
res |= r2;
if(!r2){
S[u].pb(adj);
cnt ++;
if(dp[adj] > mx){
mx2 = mx;
mx = dp[adj];
} else mx2 = max(mx2, dp[adj]);
}
}
dp[u] = cnt + mx2;
m2[u] = mx2;
m1[u] = mx;
son[u] = cnt;
if(res){
V.pb(u);
}
return res;
}
ll dp2[Maxn];
vector< pair<ll, ll> > A;
ll C[Maxn];
bool CMP(ll i, ll j){
return dp[i] > dp[j];
}
ll ps[Maxn];
bool check(ll x){
ll M = V.size();
ll c = 0;
for(int i = 0; i < M - 1; i++){
ll SS = S[V[i]].size();
for(int j = 0; j < SS; j++){
if(c + dp[S[V[i]][j]] + (SS - j) + ps[i] > x) {
c++;
} else break;
}
//cerr << c << '\n';
if(c > i + 1) return false;
}
//cerr << "!!!" << x << '\n';
return (c <= x);
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n;
cin >> n >> t >> m;
ll u, v;
for(int i = 1; i < n; i++){
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
}
DFS(t, -1);
ll sz = V.size();
dp2[sz-1] = 0;
ll sm = 0;
ps[sz-1] = 0;
for(int i = sz - 2; i >= 0; i--){
ps[i] = sm;
sm += son[V[i]];
//cerr << son[V[i]] << '\n';
ll K = 0;
sort(S[V[i]].begin(), S[V[i]].end(), CMP);
for(auto ch : S[V[i]]){
//cerr << '!' << ch << ' ' << dp[ch] << ' ' << sm << '\n';
A.pb({dp[ch] + sm - K, i + 1});
K ++;
}
dp2[i] = min( max(dp2[i + 1], sm-son[V[i]] + m1[V[i]] + son[V[i]]), 1LL + max(dp2[i + 1], sm-son[V[i]] + m2[V[i]] + son[V[i]] - 1) );
}
sort(A.begin(), A.end()); reverse(A.begin(), A.end());
ll M = A.size();
ll cnt = 1;
//cerr << '\n';
//dp2[0] = 100000000;
/*
ll ans = min(dp2[0], (M > 0 ? A[0].F : 0));
bool fl=true;;
A.pb({0, 0});
for(int i = 0; i < M; i++){
for(int j = A[i].S; j <= n; j++){
C[j] ++;
if(C[j] > j) fl = false;
}
//cerr << i << " " << fl << '\n';
if(fl) ans = min(ans, i + 1 + A[i + 1].F);
cerr << i + 1 + A[i + 1].F << '\n';
}
cout << ans << '\n';
*/
//cout << dp2[0] << '\n';
ll l = -1, r = n, mid;
while(l + 1 < r){
mid = (l + r) >> 1;
if(check(mid)) r = mid;
else l = mid;
}
//cerr << check(1) << '\n';
cout << r;
return 0;
}
/*
5 1 2
1 2
2 3
2 4
3 5
10 1 4
1 2
2 3
2 4
3 9
3 5
4 7
4 6
6 8
7 10
7 1 3
1 2
2 3
2 4
4 5
2 6
6 7
*/
Compilation message
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:109:5: warning: unused variable 'M' [-Wunused-variable]
ll M = A.size();
^
mousetrap.cpp:110:5: warning: unused variable 'cnt' [-Wunused-variable]
ll cnt = 1;
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
47480 KB |
Output is correct |
2 |
Correct |
47 ms |
47352 KB |
Output is correct |
3 |
Correct |
49 ms |
47352 KB |
Output is correct |
4 |
Correct |
47 ms |
47352 KB |
Output is correct |
5 |
Correct |
48 ms |
47352 KB |
Output is correct |
6 |
Correct |
70 ms |
47352 KB |
Output is correct |
7 |
Correct |
48 ms |
47352 KB |
Output is correct |
8 |
Correct |
47 ms |
47352 KB |
Output is correct |
9 |
Correct |
47 ms |
47352 KB |
Output is correct |
10 |
Correct |
50 ms |
47324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
628 ms |
134888 KB |
Output is correct |
2 |
Correct |
638 ms |
126356 KB |
Output is correct |
3 |
Correct |
1407 ms |
136200 KB |
Output is correct |
4 |
Correct |
669 ms |
92600 KB |
Output is correct |
5 |
Correct |
1481 ms |
136244 KB |
Output is correct |
6 |
Correct |
1395 ms |
136028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
47480 KB |
Output is correct |
2 |
Correct |
47 ms |
47352 KB |
Output is correct |
3 |
Correct |
49 ms |
47352 KB |
Output is correct |
4 |
Correct |
47 ms |
47352 KB |
Output is correct |
5 |
Correct |
48 ms |
47352 KB |
Output is correct |
6 |
Correct |
70 ms |
47352 KB |
Output is correct |
7 |
Correct |
48 ms |
47352 KB |
Output is correct |
8 |
Correct |
47 ms |
47352 KB |
Output is correct |
9 |
Correct |
47 ms |
47352 KB |
Output is correct |
10 |
Correct |
50 ms |
47324 KB |
Output is correct |
11 |
Correct |
70 ms |
47404 KB |
Output is correct |
12 |
Correct |
47 ms |
47480 KB |
Output is correct |
13 |
Correct |
47 ms |
47384 KB |
Output is correct |
14 |
Correct |
47 ms |
47608 KB |
Output is correct |
15 |
Correct |
47 ms |
47480 KB |
Output is correct |
16 |
Correct |
47 ms |
47480 KB |
Output is correct |
17 |
Correct |
47 ms |
47372 KB |
Output is correct |
18 |
Correct |
47 ms |
47480 KB |
Output is correct |
19 |
Correct |
48 ms |
47480 KB |
Output is correct |
20 |
Correct |
47 ms |
47480 KB |
Output is correct |
21 |
Correct |
49 ms |
47452 KB |
Output is correct |
22 |
Correct |
48 ms |
47480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
47480 KB |
Output is correct |
2 |
Correct |
47 ms |
47352 KB |
Output is correct |
3 |
Correct |
49 ms |
47352 KB |
Output is correct |
4 |
Correct |
47 ms |
47352 KB |
Output is correct |
5 |
Correct |
48 ms |
47352 KB |
Output is correct |
6 |
Correct |
70 ms |
47352 KB |
Output is correct |
7 |
Correct |
48 ms |
47352 KB |
Output is correct |
8 |
Correct |
47 ms |
47352 KB |
Output is correct |
9 |
Correct |
47 ms |
47352 KB |
Output is correct |
10 |
Correct |
50 ms |
47324 KB |
Output is correct |
11 |
Correct |
628 ms |
134888 KB |
Output is correct |
12 |
Correct |
638 ms |
126356 KB |
Output is correct |
13 |
Correct |
1407 ms |
136200 KB |
Output is correct |
14 |
Correct |
669 ms |
92600 KB |
Output is correct |
15 |
Correct |
1481 ms |
136244 KB |
Output is correct |
16 |
Correct |
1395 ms |
136028 KB |
Output is correct |
17 |
Correct |
70 ms |
47404 KB |
Output is correct |
18 |
Correct |
47 ms |
47480 KB |
Output is correct |
19 |
Correct |
47 ms |
47384 KB |
Output is correct |
20 |
Correct |
47 ms |
47608 KB |
Output is correct |
21 |
Correct |
47 ms |
47480 KB |
Output is correct |
22 |
Correct |
47 ms |
47480 KB |
Output is correct |
23 |
Correct |
47 ms |
47372 KB |
Output is correct |
24 |
Correct |
47 ms |
47480 KB |
Output is correct |
25 |
Correct |
48 ms |
47480 KB |
Output is correct |
26 |
Correct |
47 ms |
47480 KB |
Output is correct |
27 |
Correct |
49 ms |
47452 KB |
Output is correct |
28 |
Correct |
48 ms |
47480 KB |
Output is correct |
29 |
Correct |
47 ms |
47328 KB |
Output is correct |
30 |
Correct |
643 ms |
136372 KB |
Output is correct |
31 |
Correct |
622 ms |
136440 KB |
Output is correct |
32 |
Correct |
635 ms |
230352 KB |
Output is correct |
33 |
Correct |
602 ms |
238072 KB |
Output is correct |
34 |
Correct |
1437 ms |
137700 KB |
Output is correct |
35 |
Correct |
1524 ms |
137952 KB |
Output is correct |
36 |
Correct |
1391 ms |
152028 KB |
Output is correct |
37 |
Correct |
1399 ms |
152128 KB |
Output is correct |
38 |
Correct |
1142 ms |
148912 KB |
Output is correct |
39 |
Correct |
1141 ms |
148964 KB |
Output is correct |
40 |
Correct |
1198 ms |
148768 KB |
Output is correct |