#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef pair <int, int> ii;
const int N = 1e6 + 1;
int n, t, m;
vector <int> adj[N];
int dp[N], par[N], h[N], b[N];
bool inPath[N];
vector <ii> d;
void readInput(){
scanf("%d %d %d", &n, &t, &m);
for(int i = 1; i < n; i++){
int u, v;
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
}
void dfs(int u, int p){
for(int v : adj[u])
if (v != p){
par[v] = u;
h[v] = h[u] + 1;
dfs(v, u);
inPath[u] |= inPath[v];
}
}
void calcDp(int u, int p){
if (inPath[u]){
dp[u] = -1;
for(int v : adj[u])
if (v != p)
calcDp(v, u);
return;
}
dp[u] = 0;
int nChild = 0, max1 = 0, max2 = 0;
for(int v : adj[u])
if (v != p && !inPath[v]){
nChild++;
calcDp(v, u);
max2 = max(max2, dp[v]);
if (max1 < max2)
swap(max1, max2);
}
dp[u] = max2 + nChild;
}
void prepare(){
inPath[m] = 1;
dfs(t, -1);
inPath[m] = 0;
calcDp(t, -1);
inPath[m] = 1;
int u = -1, cnt = 0;
for(int v : adj[t])
if (v != par[t] && inPath[v])
u = v;
while (u != -1){
for(int v : adj[u])
if (v != par[u] && !inPath[v])
cnt++;
for(int v : adj[u])
if (v != par[u] && !inPath[v])
d.push_back(mp(h[m] - h[u] + 1, dp[v] + cnt));
int nxt = -1;
for(int v : adj[u])
if (v != par[u] && inPath[v])
nxt = v;
u = nxt;
}
sort(d.begin(), d.end(), [](const ii &a, const ii &b){
if (a.X != b.X)
return a.X < b.X;
return a.Y > b.Y;
});
}
bool check(int x){
int blocked = 0, cnt = 0, cur = 0;
for(ii p : d){
if (p.X != cur){
blocked += cnt;
cur = p.X;
cnt = 0;
}
if (p.Y + blocked > x){
if (blocked + cnt < min(x, p.X))
cnt++;
else
return 0;
}
}
return 1;
}
void solve(){
cerr << "d: " << endl;
for(ii p : d)
cerr << p.X << ' ' << p.Y << endl;
int l = 0, r = n - 1, res = -1;
while (l <= r){
int mid = (l + r) >> 1;
if (check(mid)){
res = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
printf("%d", res);
}
int main(){
readInput();
prepare();
solve();
return 0;
}
Compilation message
mousetrap.cpp: In function 'void readInput()':
mousetrap.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
22 | scanf("%d %d %d", &n, &t, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:25:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
25 | scanf("%d %d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
16 ms |
23788 KB |
Output is correct |
4 |
Correct |
18 ms |
23792 KB |
Output is correct |
5 |
Correct |
17 ms |
23916 KB |
Output is correct |
6 |
Correct |
15 ms |
23808 KB |
Output is correct |
7 |
Correct |
15 ms |
23788 KB |
Output is correct |
8 |
Correct |
16 ms |
23788 KB |
Output is correct |
9 |
Correct |
15 ms |
23788 KB |
Output is correct |
10 |
Correct |
15 ms |
23788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
452 ms |
80764 KB |
Output is correct |
2 |
Correct |
370 ms |
75236 KB |
Output is correct |
3 |
Correct |
1082 ms |
82344 KB |
Output is correct |
4 |
Correct |
512 ms |
52984 KB |
Output is correct |
5 |
Correct |
1082 ms |
82276 KB |
Output is correct |
6 |
Correct |
1092 ms |
82148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
16 ms |
23788 KB |
Output is correct |
4 |
Correct |
18 ms |
23792 KB |
Output is correct |
5 |
Correct |
17 ms |
23916 KB |
Output is correct |
6 |
Correct |
15 ms |
23808 KB |
Output is correct |
7 |
Correct |
15 ms |
23788 KB |
Output is correct |
8 |
Correct |
16 ms |
23788 KB |
Output is correct |
9 |
Correct |
15 ms |
23788 KB |
Output is correct |
10 |
Correct |
15 ms |
23788 KB |
Output is correct |
11 |
Correct |
16 ms |
23788 KB |
Output is correct |
12 |
Correct |
16 ms |
23916 KB |
Output is correct |
13 |
Correct |
16 ms |
23916 KB |
Output is correct |
14 |
Correct |
18 ms |
23952 KB |
Output is correct |
15 |
Correct |
16 ms |
23916 KB |
Output is correct |
16 |
Correct |
17 ms |
23916 KB |
Output is correct |
17 |
Correct |
17 ms |
23916 KB |
Output is correct |
18 |
Correct |
16 ms |
23916 KB |
Output is correct |
19 |
Correct |
16 ms |
23916 KB |
Output is correct |
20 |
Correct |
18 ms |
23916 KB |
Output is correct |
21 |
Correct |
18 ms |
23916 KB |
Output is correct |
22 |
Correct |
22 ms |
23916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
16 ms |
23788 KB |
Output is correct |
4 |
Correct |
18 ms |
23792 KB |
Output is correct |
5 |
Correct |
17 ms |
23916 KB |
Output is correct |
6 |
Correct |
15 ms |
23808 KB |
Output is correct |
7 |
Correct |
15 ms |
23788 KB |
Output is correct |
8 |
Correct |
16 ms |
23788 KB |
Output is correct |
9 |
Correct |
15 ms |
23788 KB |
Output is correct |
10 |
Correct |
15 ms |
23788 KB |
Output is correct |
11 |
Correct |
452 ms |
80764 KB |
Output is correct |
12 |
Correct |
370 ms |
75236 KB |
Output is correct |
13 |
Correct |
1082 ms |
82344 KB |
Output is correct |
14 |
Correct |
512 ms |
52984 KB |
Output is correct |
15 |
Correct |
1082 ms |
82276 KB |
Output is correct |
16 |
Correct |
1092 ms |
82148 KB |
Output is correct |
17 |
Correct |
16 ms |
23788 KB |
Output is correct |
18 |
Correct |
16 ms |
23916 KB |
Output is correct |
19 |
Correct |
16 ms |
23916 KB |
Output is correct |
20 |
Correct |
18 ms |
23952 KB |
Output is correct |
21 |
Correct |
16 ms |
23916 KB |
Output is correct |
22 |
Correct |
17 ms |
23916 KB |
Output is correct |
23 |
Correct |
17 ms |
23916 KB |
Output is correct |
24 |
Correct |
16 ms |
23916 KB |
Output is correct |
25 |
Correct |
16 ms |
23916 KB |
Output is correct |
26 |
Correct |
18 ms |
23916 KB |
Output is correct |
27 |
Correct |
18 ms |
23916 KB |
Output is correct |
28 |
Correct |
22 ms |
23916 KB |
Output is correct |
29 |
Correct |
15 ms |
23788 KB |
Output is correct |
30 |
Correct |
411 ms |
80936 KB |
Output is correct |
31 |
Correct |
405 ms |
80868 KB |
Output is correct |
32 |
Correct |
479 ms |
159652 KB |
Output is correct |
33 |
Correct |
554 ms |
159644 KB |
Output is correct |
34 |
Correct |
1157 ms |
82452 KB |
Output is correct |
35 |
Correct |
1096 ms |
82320 KB |
Output is correct |
36 |
Correct |
3202 ms |
94764 KB |
Output is correct |
37 |
Correct |
3162 ms |
94792 KB |
Output is correct |
38 |
Correct |
1775 ms |
94296 KB |
Output is correct |
39 |
Correct |
1803 ms |
94200 KB |
Output is correct |
40 |
Correct |
1833 ms |
94332 KB |
Output is correct |