#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, int> ri3;
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) begin(x), end(x)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)
const int MX_N = 1e6+5;
int N, T, M, A, B;
vector<int> al[MX_N];
int w[MX_N], d[MX_N], pa[MX_N];
vector<ii> sub[MX_N];
void dfs(int u, int p) {
for (auto v : al[u]) if (v != p) {
pa[v] = u;
d[v] = d[u] + 1;
w[v] = w[u] + (p==0?0:SZ(al[u])-2);
dfs(v,u);
}
if (SZ(al[u]) != 1) {
int a = 0, b = 0;
for (auto v : al[u]) if (v != p) {
if (w[v] > a) b = a, a = w[v];
else if (w[v] > b) b = w[v];
}
if (b) w[u] = b;
else w[u] += d[u]+1;
} else w[u] += d[u];
//cout << u << " :: " << w[u] << " " << d[u] << "\n";
}
int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> T >> M;
FOR(i,1,N-1){
cin >> A >> B;
al[A].push_back(B);
al[B].push_back(A);
}
dfs(T,0);
int lo = -1, hi = 2*N;
while (hi-lo > 1) {
int mid = (lo+hi)/2;
int p, u, i, b, bad = 0;
for (p = -1, u = M, i = 0, b = 0; pa[u] != 0; p = u, u = pa[u], ++i) {
//cout << "\tTRAVERSE " << u << endl;
int x = 0;
for (int v : al[u]) if (v != pa[u] && v != p) {
//cout << "\t\tNEI " << v << " :: " << w[v]-(u!=M)-d[u] << endl;
if (w[v]-(u!=M)-d[u]+b > mid) ++x;
}
b += x;
if (b > i+1) bad = 1;
}
if (b > mid) bad = 1;
//cout << mid << " :: " << bad << endl;
(bad ? lo : hi) = mid;
}
cout << hi << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
47352 KB |
Output is correct |
2 |
Correct |
46 ms |
47352 KB |
Output is correct |
3 |
Correct |
46 ms |
47352 KB |
Output is correct |
4 |
Correct |
46 ms |
47352 KB |
Output is correct |
5 |
Correct |
46 ms |
47340 KB |
Output is correct |
6 |
Correct |
43 ms |
47352 KB |
Output is correct |
7 |
Correct |
45 ms |
47356 KB |
Output is correct |
8 |
Correct |
45 ms |
47352 KB |
Output is correct |
9 |
Correct |
46 ms |
47352 KB |
Output is correct |
10 |
Correct |
46 ms |
47352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
443 ms |
90312 KB |
Output is correct |
2 |
Correct |
401 ms |
86136 KB |
Output is correct |
3 |
Correct |
919 ms |
91548 KB |
Output is correct |
4 |
Correct |
478 ms |
75880 KB |
Output is correct |
5 |
Correct |
976 ms |
104664 KB |
Output is correct |
6 |
Correct |
945 ms |
104828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
47352 KB |
Output is correct |
2 |
Correct |
46 ms |
47352 KB |
Output is correct |
3 |
Correct |
46 ms |
47352 KB |
Output is correct |
4 |
Correct |
46 ms |
47352 KB |
Output is correct |
5 |
Correct |
46 ms |
47340 KB |
Output is correct |
6 |
Correct |
43 ms |
47352 KB |
Output is correct |
7 |
Correct |
45 ms |
47356 KB |
Output is correct |
8 |
Correct |
45 ms |
47352 KB |
Output is correct |
9 |
Correct |
46 ms |
47352 KB |
Output is correct |
10 |
Correct |
46 ms |
47352 KB |
Output is correct |
11 |
Correct |
46 ms |
47372 KB |
Output is correct |
12 |
Correct |
54 ms |
47352 KB |
Output is correct |
13 |
Correct |
46 ms |
47352 KB |
Output is correct |
14 |
Correct |
52 ms |
47460 KB |
Output is correct |
15 |
Correct |
46 ms |
47480 KB |
Output is correct |
16 |
Correct |
46 ms |
47352 KB |
Output is correct |
17 |
Correct |
46 ms |
47352 KB |
Output is correct |
18 |
Correct |
42 ms |
47480 KB |
Output is correct |
19 |
Correct |
46 ms |
47480 KB |
Output is correct |
20 |
Correct |
46 ms |
47428 KB |
Output is correct |
21 |
Correct |
47 ms |
47352 KB |
Output is correct |
22 |
Correct |
46 ms |
47352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
47352 KB |
Output is correct |
2 |
Correct |
46 ms |
47352 KB |
Output is correct |
3 |
Correct |
46 ms |
47352 KB |
Output is correct |
4 |
Correct |
46 ms |
47352 KB |
Output is correct |
5 |
Correct |
46 ms |
47340 KB |
Output is correct |
6 |
Correct |
43 ms |
47352 KB |
Output is correct |
7 |
Correct |
45 ms |
47356 KB |
Output is correct |
8 |
Correct |
45 ms |
47352 KB |
Output is correct |
9 |
Correct |
46 ms |
47352 KB |
Output is correct |
10 |
Correct |
46 ms |
47352 KB |
Output is correct |
11 |
Correct |
443 ms |
90312 KB |
Output is correct |
12 |
Correct |
401 ms |
86136 KB |
Output is correct |
13 |
Correct |
919 ms |
91548 KB |
Output is correct |
14 |
Correct |
478 ms |
75880 KB |
Output is correct |
15 |
Correct |
976 ms |
104664 KB |
Output is correct |
16 |
Correct |
945 ms |
104828 KB |
Output is correct |
17 |
Correct |
46 ms |
47372 KB |
Output is correct |
18 |
Correct |
54 ms |
47352 KB |
Output is correct |
19 |
Correct |
46 ms |
47352 KB |
Output is correct |
20 |
Correct |
52 ms |
47460 KB |
Output is correct |
21 |
Correct |
46 ms |
47480 KB |
Output is correct |
22 |
Correct |
46 ms |
47352 KB |
Output is correct |
23 |
Correct |
46 ms |
47352 KB |
Output is correct |
24 |
Correct |
42 ms |
47480 KB |
Output is correct |
25 |
Correct |
46 ms |
47480 KB |
Output is correct |
26 |
Correct |
46 ms |
47428 KB |
Output is correct |
27 |
Correct |
47 ms |
47352 KB |
Output is correct |
28 |
Correct |
46 ms |
47352 KB |
Output is correct |
29 |
Correct |
47 ms |
47436 KB |
Output is correct |
30 |
Correct |
443 ms |
103680 KB |
Output is correct |
31 |
Correct |
442 ms |
103872 KB |
Output is correct |
32 |
Correct |
627 ms |
182320 KB |
Output is correct |
33 |
Correct |
532 ms |
182136 KB |
Output is correct |
34 |
Correct |
928 ms |
104668 KB |
Output is correct |
35 |
Correct |
1086 ms |
104776 KB |
Output is correct |
36 |
Correct |
1029 ms |
112556 KB |
Output is correct |
37 |
Correct |
1032 ms |
112504 KB |
Output is correct |
38 |
Correct |
848 ms |
115576 KB |
Output is correct |
39 |
Correct |
808 ms |
115388 KB |
Output is correct |
40 |
Correct |
845 ms |
115236 KB |
Output is correct |