#include <bits/stdc++.h>
#define FOR(i, l, r) for(int i=(l); i<=(r); ++i)
#define REP(i, l, r) for(int i=(l); i<(r); ++i)
#define FORD(i, r, l) for(int i=(r); i>=(l); --i)
#define REPD(i, r, l) for(int i=(r)-1; i>=(l); --i)
using namespace std;
const int N = 1e6 + 5;
int n, trap, mouse, cstep[N], deg[N], sdeg[N], p[N], avail[N];
vector<int> S[N], Q[N];
void Enter() {
cin >> n >> trap >> mouse;
REP(i, 1, n) {
int u, v;
cin >> u >> v;
S[u].push_back(v);
S[v].push_back(u);
}
}
void DFS(int u, int pa) {
if (u == mouse) avail[u] = 1;
p[u] = pa;
for(int v : S[u]) if (v != pa) {
DFS(v, u);
if (avail[v]) avail[u] = 1;
else {
Q[u].push_back(v);
++deg[u];
}
}
sort(Q[u].begin(), Q[u].end(), [] (int x, int y) { return cstep[x] > cstep[y]; });
if (deg[u] <= 1) cstep[u] = deg[u];
if (deg[u] > 1) cstep[u] = cstep[Q[u][1]] + deg[u];
}
void DFS2(int u) {
cstep[u] += sdeg[u];
for(int v : S[u]) if (v != p[u]) {
sdeg[v] = deg[u] + sdeg[u];
DFS2(v);
}
}
bool check(int x) {
int sl = 0, timer = 0;
for(int i = mouse; i != trap; i = p[i]) {
++timer;
int c_now = 0;
for(int v : Q[i]) if (cstep[v] + sl - c_now > x) ++sl, ++c_now;
if (sl > timer || sl > x) return false;
}
return true;
}
void Process() {
DFS(trap, 0);
deg[trap] = 0;
DFS2(trap);
int l = 0, r = n, mid;
while (l < r) {
mid = (l + r) / 2;
if (check(mid)) r = mid;
else l = mid + 1;
}
cout << l;
}
int main()
{
#define file "mousetrap"
if (fopen(file".inp", "r")) {
freopen(file".inp", "r", stdin);
freopen(file".out", "w", stdout);
}
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
Enter();
Process();
}
Compilation message
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(file".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen(file".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
47296 KB |
Output is correct |
2 |
Correct |
24 ms |
47292 KB |
Output is correct |
3 |
Correct |
24 ms |
47312 KB |
Output is correct |
4 |
Correct |
28 ms |
47316 KB |
Output is correct |
5 |
Correct |
25 ms |
47296 KB |
Output is correct |
6 |
Correct |
24 ms |
47308 KB |
Output is correct |
7 |
Correct |
27 ms |
47304 KB |
Output is correct |
8 |
Correct |
28 ms |
47224 KB |
Output is correct |
9 |
Correct |
25 ms |
47308 KB |
Output is correct |
10 |
Correct |
26 ms |
47272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
385 ms |
121284 KB |
Output is correct |
2 |
Correct |
333 ms |
113920 KB |
Output is correct |
3 |
Correct |
1034 ms |
124724 KB |
Output is correct |
4 |
Correct |
523 ms |
85796 KB |
Output is correct |
5 |
Correct |
1063 ms |
124732 KB |
Output is correct |
6 |
Correct |
1067 ms |
124856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
47296 KB |
Output is correct |
2 |
Correct |
24 ms |
47292 KB |
Output is correct |
3 |
Correct |
24 ms |
47312 KB |
Output is correct |
4 |
Correct |
28 ms |
47316 KB |
Output is correct |
5 |
Correct |
25 ms |
47296 KB |
Output is correct |
6 |
Correct |
24 ms |
47308 KB |
Output is correct |
7 |
Correct |
27 ms |
47304 KB |
Output is correct |
8 |
Correct |
28 ms |
47224 KB |
Output is correct |
9 |
Correct |
25 ms |
47308 KB |
Output is correct |
10 |
Correct |
26 ms |
47272 KB |
Output is correct |
11 |
Correct |
29 ms |
47316 KB |
Output is correct |
12 |
Correct |
30 ms |
47308 KB |
Output is correct |
13 |
Correct |
25 ms |
47372 KB |
Output is correct |
14 |
Correct |
26 ms |
47376 KB |
Output is correct |
15 |
Correct |
25 ms |
47444 KB |
Output is correct |
16 |
Correct |
26 ms |
47316 KB |
Output is correct |
17 |
Correct |
27 ms |
47308 KB |
Output is correct |
18 |
Correct |
25 ms |
47320 KB |
Output is correct |
19 |
Correct |
24 ms |
47292 KB |
Output is correct |
20 |
Correct |
24 ms |
47316 KB |
Output is correct |
21 |
Correct |
25 ms |
47308 KB |
Output is correct |
22 |
Correct |
29 ms |
47276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
47296 KB |
Output is correct |
2 |
Correct |
24 ms |
47292 KB |
Output is correct |
3 |
Correct |
24 ms |
47312 KB |
Output is correct |
4 |
Correct |
28 ms |
47316 KB |
Output is correct |
5 |
Correct |
25 ms |
47296 KB |
Output is correct |
6 |
Correct |
24 ms |
47308 KB |
Output is correct |
7 |
Correct |
27 ms |
47304 KB |
Output is correct |
8 |
Correct |
28 ms |
47224 KB |
Output is correct |
9 |
Correct |
25 ms |
47308 KB |
Output is correct |
10 |
Correct |
26 ms |
47272 KB |
Output is correct |
11 |
Correct |
385 ms |
121284 KB |
Output is correct |
12 |
Correct |
333 ms |
113920 KB |
Output is correct |
13 |
Correct |
1034 ms |
124724 KB |
Output is correct |
14 |
Correct |
523 ms |
85796 KB |
Output is correct |
15 |
Correct |
1063 ms |
124732 KB |
Output is correct |
16 |
Correct |
1067 ms |
124856 KB |
Output is correct |
17 |
Correct |
29 ms |
47316 KB |
Output is correct |
18 |
Correct |
30 ms |
47308 KB |
Output is correct |
19 |
Correct |
25 ms |
47372 KB |
Output is correct |
20 |
Correct |
26 ms |
47376 KB |
Output is correct |
21 |
Correct |
25 ms |
47444 KB |
Output is correct |
22 |
Correct |
26 ms |
47316 KB |
Output is correct |
23 |
Correct |
27 ms |
47308 KB |
Output is correct |
24 |
Correct |
25 ms |
47320 KB |
Output is correct |
25 |
Correct |
24 ms |
47292 KB |
Output is correct |
26 |
Correct |
24 ms |
47316 KB |
Output is correct |
27 |
Correct |
25 ms |
47308 KB |
Output is correct |
28 |
Correct |
29 ms |
47276 KB |
Output is correct |
29 |
Correct |
24 ms |
47268 KB |
Output is correct |
30 |
Correct |
342 ms |
121324 KB |
Output is correct |
31 |
Correct |
373 ms |
121432 KB |
Output is correct |
32 |
Correct |
425 ms |
186080 KB |
Output is correct |
33 |
Correct |
492 ms |
217292 KB |
Output is correct |
34 |
Correct |
1114 ms |
124824 KB |
Output is correct |
35 |
Correct |
1049 ms |
124792 KB |
Output is correct |
36 |
Correct |
1042 ms |
132428 KB |
Output is correct |
37 |
Correct |
1064 ms |
132544 KB |
Output is correct |
38 |
Correct |
797 ms |
124216 KB |
Output is correct |
39 |
Correct |
759 ms |
124164 KB |
Output is correct |
40 |
Correct |
751 ms |
124200 KB |
Output is correct |