#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 1e6 + 50;
int n, t, m, val[N], dp[N], ps[N], k;
bool mark[N];
vector<int> adj[N];
vector<pii> vc;
void dfs(int u, int p=0) {
if (u == t) return;
int mx1 = 0, mx2 = 0;
for (int v:adj[u]) {
if (v == p) continue;
dfs(v, u);
if (mark[v]) {
mark[u] = 1;
val[u] = val[v] + adj[u].size()-1-(p>0);
}
else {
if (dp[v] > mx1) {
mx2 = mx1;
mx1 = dp[v];
}
else if (dp[v] > mx2) {
mx2 = dp[v];
}
}
}
if (!mark[u]) {
dp[u] = adj[u].size()-(p>0) + mx2;
}
else {
for (int v:adj[u]) {
if (v == p) continue;
if (mark[v]) continue;
vc.push_back({u, dp[v] + val[u]});
}
}
}
bool isval(int x) {
int prv = 0;
int tmp = 0;
for (pii p:vc) {
if (p.F != prv) {
ps[p.F] = ps[prv];
tmp = ps[prv];
prv = p.F;
}
if (tmp+p.S > x) {
ps[p.F]++;
}
if (ps[p.F] > p.F) return 0;
}
return 1;
}
void solve() {
cin >> n >> t >> m;
for (int i = 1; i <= n-1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
mark[t] = 1;
dfs(m);
reverse(vc.begin(), vc.end());
int prv= 0;
for (pii &x : vc) {
if (x.F != prv) {
prv = x.F;
k++;
}
x.F = k;
}
int dw = -1, up = N;
while (up-dw > 1) {
int md = (up+dw)>>1;
if (isval(md)) up = md;
else dw = md;
}
cout << up << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
23700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
281 ms |
64340 KB |
Output is correct |
2 |
Correct |
254 ms |
60672 KB |
Output is correct |
3 |
Correct |
631 ms |
65284 KB |
Output is correct |
4 |
Correct |
295 ms |
47532 KB |
Output is correct |
5 |
Correct |
622 ms |
65404 KB |
Output is correct |
6 |
Correct |
623 ms |
65208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
23700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
23700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |