#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fr first
#define sc second
#define endl '\n'
const int MAXN = 1000100;
int n, t, m;
vector<int> adj[MAXN];
int dp[MAXN];
int par[MAXN];
int prv[MAXN], nxt[MAXN];
vector<int> path;
vector<int> dpvec[MAXN];
void dfs1(int v)
{
for (int u : adj[v]) if (u != par[v]) par[u] = v, dfs1(u);
}
void dfs2(int v)
{
vector<int> vec;
for (int u : adj[v]) if (u != par[v] && u != prv[v] && u != nxt[v]) dfs2(u), vec.pb(dp[u]);
sort(all(vec), greater<int>());
dp[v] = vec.size() + ((int)vec.size() >= 2 ? vec[1] : 0);
}
bool check(int x)
{
int blk = 0;
FOR(i, 0, (int)path.size())
{
int v = path[i];
int cur = 0;
for (int u : dpvec[v]) if (blk + u > x) cur++;
blk += cur;
if (blk > i + 1) return false;
}
return true;
}
int32_t main(void)
{
fast_io;
cin >> n >> t >> m;
if (m == t) return cout << "0\n", 0;
FOR(i, 0, n - 1)
{
int x, y;
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
dfs1(m);
int v = t;
while (v != m)
{
nxt[par[v]] = v;
prv[v] = par[v];
path.pb(v);
v = par[v];
}
path.pb(m);
reverse(all(path));
path.pop_back();
for (int u : path) dfs2(u);
dbgr(dp + 1, dp + n + 1);
int sm = 0;
for (int i = (int)path.size() - 1; i >= 0; i--)
{
int v = path[i];
for (int u : adj[v]) if (v != prv[u] && v != nxt[u]) dpvec[v].pb(dp[u]);
sm += dpvec[v].size();
FOR(j, 0, (int)dpvec[v].size()) dpvec[v][j] += sm;
}
int l = -1, r = 3 * n;
while (r - l > 1)
{
int mid = (r + l) / 2;
if (check(mid)) r = mid;
else l = mid;
}
cout << r << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
47308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
410 ms |
88976 KB |
Output is correct |
2 |
Correct |
363 ms |
85204 KB |
Output is correct |
3 |
Correct |
1115 ms |
90180 KB |
Output is correct |
4 |
Correct |
506 ms |
69620 KB |
Output is correct |
5 |
Correct |
1106 ms |
90184 KB |
Output is correct |
6 |
Correct |
1080 ms |
90232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
47308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
47308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |