#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 1e6 + 5;
int dp[nmax], p[nmax];
vector<int> g[nmax];
vector<vector<int>> adj;
void initp(int node, int f) {
p[node] = f;
for(auto x : g[node]) if(x != f) initp(x, node);
}
void initdp(int node, int f) {
//cerr << node << '\n';
vector<int> vs;
for(auto x : g[node]) {
if(x == f) continue;
initdp(x, node);
vs.emplace_back(dp[x]);
}
sort(all(vs));
if(sz(vs) < 2) dp[node] = sz(vs);
else dp[node] = sz(vs) + rbegin(vs)[1];
return;
}
bool crapa(int threshold) {
int tolerance = 1;
for(int i = 0; i < sz(adj); tolerance++, i++) {
int cnt = 0;
for(auto x : adj[i])
if(x > threshold) cnt++, tolerance--;
else break;
threshold -= cnt;
if(tolerance < 0 || threshold < 0) return 1;
}
return 0;
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int n, T, M;
cin >> n >> T >> M;
for(int i = 1, x, y; i < n; i++) {
cin >> x >> y;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
initp(T, T);
int prv = -1;
while(M != T) {
adj.emplace_back();
for(auto x : g[M]) {
if(x == p[M] || x == prv) continue;
initdp(x, M);
adj.back().emplace_back(dp[x]);
}
prv = M;
M = p[M];
sort(all(adj.back()), greater<int>());
}
//for(auto x : adj) {
//for(auto y : x) cerr << y << ' ';
//cerr << '\n';
//}
int sum = 0;
for(int i = 0; i < sz(adj); i++) {
sum += sz(rbegin(adj)[i]);
for(auto &x : rbegin(adj)[i]) x += sum;
}
//for(auto x : adj) {
//for(auto y : x) cerr << y << ' ';
//cerr << '\n';
//}
int lim = -1;
for(int step = 21; step >= 0; step--)
if(crapa(lim + (1 << step))) lim += (1 << step);
cout << lim + 1 << '\n';;
}
/**
Anul asta se da centroid.
-- Surse oficiale
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23816 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23768 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23776 KB |
Output is correct |
10 |
Correct |
12 ms |
23820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
256 ms |
76276 KB |
Output is correct |
2 |
Correct |
228 ms |
70904 KB |
Output is correct |
3 |
Correct |
815 ms |
77204 KB |
Output is correct |
4 |
Correct |
358 ms |
50332 KB |
Output is correct |
5 |
Correct |
763 ms |
77168 KB |
Output is correct |
6 |
Correct |
877 ms |
77236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23816 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23768 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23776 KB |
Output is correct |
10 |
Correct |
12 ms |
23820 KB |
Output is correct |
11 |
Correct |
10 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23820 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23916 KB |
Output is correct |
15 |
Correct |
12 ms |
23800 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23872 KB |
Output is correct |
19 |
Correct |
10 ms |
23764 KB |
Output is correct |
20 |
Correct |
10 ms |
23764 KB |
Output is correct |
21 |
Correct |
10 ms |
23828 KB |
Output is correct |
22 |
Correct |
10 ms |
23816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23816 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23768 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23776 KB |
Output is correct |
10 |
Correct |
12 ms |
23820 KB |
Output is correct |
11 |
Correct |
256 ms |
76276 KB |
Output is correct |
12 |
Correct |
228 ms |
70904 KB |
Output is correct |
13 |
Correct |
815 ms |
77204 KB |
Output is correct |
14 |
Correct |
358 ms |
50332 KB |
Output is correct |
15 |
Correct |
763 ms |
77168 KB |
Output is correct |
16 |
Correct |
877 ms |
77236 KB |
Output is correct |
17 |
Correct |
10 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23820 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23916 KB |
Output is correct |
21 |
Correct |
12 ms |
23800 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
11 ms |
23764 KB |
Output is correct |
24 |
Correct |
11 ms |
23872 KB |
Output is correct |
25 |
Correct |
10 ms |
23764 KB |
Output is correct |
26 |
Correct |
10 ms |
23764 KB |
Output is correct |
27 |
Correct |
10 ms |
23828 KB |
Output is correct |
28 |
Correct |
10 ms |
23816 KB |
Output is correct |
29 |
Correct |
12 ms |
23812 KB |
Output is correct |
30 |
Correct |
247 ms |
76236 KB |
Output is correct |
31 |
Correct |
241 ms |
76216 KB |
Output is correct |
32 |
Correct |
292 ms |
119792 KB |
Output is correct |
33 |
Correct |
291 ms |
185932 KB |
Output is correct |
34 |
Correct |
850 ms |
77176 KB |
Output is correct |
35 |
Correct |
889 ms |
77208 KB |
Output is correct |
36 |
Correct |
905 ms |
84300 KB |
Output is correct |
37 |
Correct |
897 ms |
84232 KB |
Output is correct |
38 |
Correct |
578 ms |
86224 KB |
Output is correct |
39 |
Correct |
615 ms |
86200 KB |
Output is correct |
40 |
Correct |
612 ms |
86176 KB |
Output is correct |