#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 1e6 + 5;
int n, t, m, u, v, d, dp[NMAX], inP[NMAX];
vector<int> adj[NMAX], P;
int dfs(int x, int p){
int ret = x == m;
for(int nx : adj[x]){
if(nx == p) continue;
ret |= dfs(nx, x);
}
if(ret) P.emplace_back(x), inP[x] = 1;
return ret;
}
void dfs2(int x, int p){
int deg, mx1, mx2, t;
deg = mx1 = mx2 = 0;
for(int nx : adj[x]){
if(nx == p) continue;
dfs2(nx, x); deg++;
t = dp[nx];
if(t > mx2) mx2 = t;
if(mx2 > mx1) swap(mx1, mx2);
}
dp[x] = mx2 + deg;
return;
}
int go(int m){
int cnt = 0, b = 0, deg = d;
for(int p : P){
int t = 0; cnt++;
for(int x: adj[p]) {
if(inP[x]) continue;
if(dp[x] + deg > m) b++;
else t++;
}
if(b > cnt) return 0;
deg -= t;
}
return 1;
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> t >> m;
for(int i = 0; i < n - 1; i++){
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
dfs(t, -1); P.pop_back();
for(int p : P)
for(int x : adj[p])
if(!inP[x]) dfs2(x, p), d++;
int l = d, r = 2 * n;
while(l < r){
int m = (l + r) >> 1;
if(go(m)) r = m;
else l = m + 1;
}
cout << l;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23728 KB |
Output is correct |
3 |
Correct |
13 ms |
23756 KB |
Output is correct |
4 |
Correct |
13 ms |
23812 KB |
Output is correct |
5 |
Correct |
13 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
12 ms |
23788 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
322 ms |
72316 KB |
Output is correct |
2 |
Correct |
244 ms |
67472 KB |
Output is correct |
3 |
Correct |
789 ms |
73304 KB |
Output is correct |
4 |
Correct |
346 ms |
48440 KB |
Output is correct |
5 |
Correct |
847 ms |
73368 KB |
Output is correct |
6 |
Correct |
756 ms |
73268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23728 KB |
Output is correct |
3 |
Correct |
13 ms |
23756 KB |
Output is correct |
4 |
Correct |
13 ms |
23812 KB |
Output is correct |
5 |
Correct |
13 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
12 ms |
23788 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23816 KB |
Output is correct |
12 |
Correct |
13 ms |
23776 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23892 KB |
Output is correct |
15 |
Correct |
13 ms |
23892 KB |
Output is correct |
16 |
Correct |
14 ms |
23764 KB |
Output is correct |
17 |
Correct |
15 ms |
23772 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
19 |
Correct |
14 ms |
23824 KB |
Output is correct |
20 |
Correct |
13 ms |
23856 KB |
Output is correct |
21 |
Correct |
13 ms |
23816 KB |
Output is correct |
22 |
Correct |
12 ms |
23844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23728 KB |
Output is correct |
3 |
Correct |
13 ms |
23756 KB |
Output is correct |
4 |
Correct |
13 ms |
23812 KB |
Output is correct |
5 |
Correct |
13 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
12 ms |
23788 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
322 ms |
72316 KB |
Output is correct |
12 |
Correct |
244 ms |
67472 KB |
Output is correct |
13 |
Correct |
789 ms |
73304 KB |
Output is correct |
14 |
Correct |
346 ms |
48440 KB |
Output is correct |
15 |
Correct |
847 ms |
73368 KB |
Output is correct |
16 |
Correct |
756 ms |
73268 KB |
Output is correct |
17 |
Correct |
14 ms |
23816 KB |
Output is correct |
18 |
Correct |
13 ms |
23776 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
13 ms |
23892 KB |
Output is correct |
21 |
Correct |
13 ms |
23892 KB |
Output is correct |
22 |
Correct |
14 ms |
23764 KB |
Output is correct |
23 |
Correct |
15 ms |
23772 KB |
Output is correct |
24 |
Correct |
13 ms |
23764 KB |
Output is correct |
25 |
Correct |
14 ms |
23824 KB |
Output is correct |
26 |
Correct |
13 ms |
23856 KB |
Output is correct |
27 |
Correct |
13 ms |
23816 KB |
Output is correct |
28 |
Correct |
12 ms |
23844 KB |
Output is correct |
29 |
Correct |
14 ms |
23764 KB |
Output is correct |
30 |
Correct |
277 ms |
72392 KB |
Output is correct |
31 |
Correct |
289 ms |
72324 KB |
Output is correct |
32 |
Correct |
374 ms |
139028 KB |
Output is correct |
33 |
Correct |
346 ms |
166320 KB |
Output is correct |
34 |
Correct |
828 ms |
73336 KB |
Output is correct |
35 |
Correct |
831 ms |
73364 KB |
Output is correct |
36 |
Correct |
864 ms |
80088 KB |
Output is correct |
37 |
Correct |
857 ms |
80032 KB |
Output is correct |
38 |
Correct |
557 ms |
82740 KB |
Output is correct |
39 |
Correct |
641 ms |
82788 KB |
Output is correct |
40 |
Correct |
655 ms |
82736 KB |
Output is correct |