/** I can do this all day **/
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define Mp make_pair
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
const int N = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 22;
ll pw(ll a , ll b, ll M) { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }
int n, root, m, dp[N], cost[N], P[LOG][N], H[N];
vector < int > G[N];
int LCA(int v, int u)
{
if(H[v] > H[u]) swap(u, v);
int del = H[u] - H[v];
for(int T = 0; T < LOG; T ++) if(del >> T & 1) u = P[T][u];
if(v == u) return v;
for(int T = LOG - 1; ~T; T --)
{
if(P[T][v] ^ P[T][u])
{
u = P[T][u];
v = P[T][v];
}
}
return P[0][v];
}
void pre(int v)
{
if(v ^ root)
{
cost[v] = cost[P[0][v]] + SZ(G[v]) - 1;
}
H[v] = H[P[0][v]] + 1;
for(int T = 1; T < LOG; T ++) P[T][v] = P[T - 1][P[T - 1][v]];
for(auto u : G[v])
{
if(u == P[0][v]) continue;
P[0][u] = v;
pre(u);
}
}
void dfs(int v)
{
if(v == root)
{
cost[v] = 0;
}
else
{
cost[v] = cost[v] - H[LCA(v, m)] + 1;
if(LCA(v, m) == m) cost[v] ++;
}
int Mx1 = cost[v], Mx2 = cost[v], cnt = 0;
for(auto u : G[v])
{
if(u == P[0][v]) continue;
dfs(u);
cnt ++;
if(dp[u] > Mx1)
{
Mx2 = Mx1;
Mx1 = dp[u];
}
else
{
Mx2 = max(Mx2, dp[u]);
}
}
if(v == root)
{
dp[v] = 0;
return;
}
if(cnt <= 1)
{
dp[v] = cost[v];
}
else
{
dp[v] = Mx2;
}
}
bool check(int x)
{
if(x < 0) return 0;
int s = 1, node = m, bads = 0;
for(auto u : G[node])
{
if(u == P[0][node]) continue;
if(dp[u] > x)
{
bads ++;
}
}
s -= bads;
x -= bads;
if(s < 0 || x < 0) return 0;
int last = m;
node = P[0][node];
s ++;
while(node ^ root)
{
bads = 0;
for(auto u : G[node])
{
if(u == last || u == P[0][node]) continue;
if(dp[u] > x)
{
bads ++;
}
}
s -= bads;
x -= bads;
if(x < 0 || s < 0) return 0;
last = node;
node = P[0][node];
s ++;
}
return 1;
}
int main()
{
scanf("%d%d%d", &n, &root, &m);
if(root == m) return !printf("0");
for(int i = 1; i < n; i ++)
{
int a, b;
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
pre(root);
dfs(root);
/*for(int i = 1; i <= n; i ++)
{
printf("i = %d cost : %d dp : %d\n", i, cost[i], dp[i]);
}
*/
int d = -1, up = n + 10;
while(up - d > 1)
{
int mid = (up + d) >> 1;
if(check(mid))
{
up = mid;
}
else
{
d = mid;
}
}
printf("%d", up);
return 0;
}
/** test corner cases(n = 1?) watch for overflow or minus indices **/
Compilation message
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:147:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
147 | scanf("%d%d%d", &n, &root, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:152:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
152 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
24044 KB |
Output is correct |
2 |
Correct |
16 ms |
24044 KB |
Output is correct |
3 |
Correct |
16 ms |
24044 KB |
Output is correct |
4 |
Correct |
16 ms |
24044 KB |
Output is correct |
5 |
Correct |
15 ms |
24044 KB |
Output is correct |
6 |
Correct |
15 ms |
24044 KB |
Output is correct |
7 |
Correct |
15 ms |
24044 KB |
Output is correct |
8 |
Correct |
16 ms |
24044 KB |
Output is correct |
9 |
Correct |
15 ms |
24044 KB |
Output is correct |
10 |
Correct |
16 ms |
24044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
604 ms |
153068 KB |
Output is correct |
2 |
Correct |
557 ms |
140140 KB |
Output is correct |
3 |
Correct |
1561 ms |
154092 KB |
Output is correct |
4 |
Correct |
724 ms |
89324 KB |
Output is correct |
5 |
Correct |
1545 ms |
154204 KB |
Output is correct |
6 |
Correct |
1554 ms |
154092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
24044 KB |
Output is correct |
2 |
Correct |
16 ms |
24044 KB |
Output is correct |
3 |
Correct |
16 ms |
24044 KB |
Output is correct |
4 |
Correct |
16 ms |
24044 KB |
Output is correct |
5 |
Correct |
15 ms |
24044 KB |
Output is correct |
6 |
Correct |
15 ms |
24044 KB |
Output is correct |
7 |
Correct |
15 ms |
24044 KB |
Output is correct |
8 |
Correct |
16 ms |
24044 KB |
Output is correct |
9 |
Correct |
15 ms |
24044 KB |
Output is correct |
10 |
Correct |
16 ms |
24044 KB |
Output is correct |
11 |
Correct |
16 ms |
24044 KB |
Output is correct |
12 |
Correct |
16 ms |
24172 KB |
Output is correct |
13 |
Correct |
17 ms |
24172 KB |
Output is correct |
14 |
Correct |
19 ms |
24336 KB |
Output is correct |
15 |
Correct |
17 ms |
24172 KB |
Output is correct |
16 |
Correct |
16 ms |
24172 KB |
Output is correct |
17 |
Correct |
19 ms |
24172 KB |
Output is correct |
18 |
Correct |
19 ms |
24172 KB |
Output is correct |
19 |
Correct |
16 ms |
24172 KB |
Output is correct |
20 |
Correct |
17 ms |
24192 KB |
Output is correct |
21 |
Correct |
17 ms |
24140 KB |
Output is correct |
22 |
Correct |
16 ms |
24044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
24044 KB |
Output is correct |
2 |
Correct |
16 ms |
24044 KB |
Output is correct |
3 |
Correct |
16 ms |
24044 KB |
Output is correct |
4 |
Correct |
16 ms |
24044 KB |
Output is correct |
5 |
Correct |
15 ms |
24044 KB |
Output is correct |
6 |
Correct |
15 ms |
24044 KB |
Output is correct |
7 |
Correct |
15 ms |
24044 KB |
Output is correct |
8 |
Correct |
16 ms |
24044 KB |
Output is correct |
9 |
Correct |
15 ms |
24044 KB |
Output is correct |
10 |
Correct |
16 ms |
24044 KB |
Output is correct |
11 |
Correct |
604 ms |
153068 KB |
Output is correct |
12 |
Correct |
557 ms |
140140 KB |
Output is correct |
13 |
Correct |
1561 ms |
154092 KB |
Output is correct |
14 |
Correct |
724 ms |
89324 KB |
Output is correct |
15 |
Correct |
1545 ms |
154204 KB |
Output is correct |
16 |
Correct |
1554 ms |
154092 KB |
Output is correct |
17 |
Correct |
16 ms |
24044 KB |
Output is correct |
18 |
Correct |
16 ms |
24172 KB |
Output is correct |
19 |
Correct |
17 ms |
24172 KB |
Output is correct |
20 |
Correct |
19 ms |
24336 KB |
Output is correct |
21 |
Correct |
17 ms |
24172 KB |
Output is correct |
22 |
Correct |
16 ms |
24172 KB |
Output is correct |
23 |
Correct |
19 ms |
24172 KB |
Output is correct |
24 |
Correct |
19 ms |
24172 KB |
Output is correct |
25 |
Correct |
16 ms |
24172 KB |
Output is correct |
26 |
Correct |
17 ms |
24192 KB |
Output is correct |
27 |
Correct |
17 ms |
24140 KB |
Output is correct |
28 |
Correct |
16 ms |
24044 KB |
Output is correct |
29 |
Correct |
16 ms |
24044 KB |
Output is correct |
30 |
Correct |
672 ms |
166460 KB |
Output is correct |
31 |
Correct |
747 ms |
166508 KB |
Output is correct |
32 |
Correct |
780 ms |
260744 KB |
Output is correct |
33 |
Correct |
702 ms |
260508 KB |
Output is correct |
34 |
Correct |
1794 ms |
167476 KB |
Output is correct |
35 |
Correct |
1770 ms |
167404 KB |
Output is correct |
36 |
Correct |
1927 ms |
176748 KB |
Output is correct |
37 |
Correct |
1940 ms |
176892 KB |
Output is correct |
38 |
Correct |
1730 ms |
179460 KB |
Output is correct |
39 |
Correct |
1697 ms |
179476 KB |
Output is correct |
40 |
Correct |
1693 ms |
179752 KB |
Output is correct |