This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
void Hollwo_Pelw();
signed main(){
#ifndef hollwo_pelw_local
if (fopen("mousetrap.inp", "r"))
assert(freopen("mousetrap.inp", "r", stdin)), assert(freopen("mousetrap.out", "w", stdout));
#else
auto start = chrono::steady_clock::now();
#endif
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = chrono::steady_clock::now();
cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
const int N = 1e6 + 5;
int n, s, t, deg[N], dp[N], par[N], in_path[N];
vector<int> adj[N];
void find_path(int u, int p) {
par[u] = p;
for (int v : adj[u])
if (v != p) find_path(v, u);
}
void calc_dp(int u, int p) {
int m1 = 0, m2 = 0;
for (int v : adj[u]) if (v != p) {
deg[u] ++;
calc_dp(v, u);
int f = dp[v];
if (f > m1) swap(f, m1);
if (f > m2) swap(f, m2);
}
dp[u] = deg[u] + m2;
}
bool check(int mid) {
int tot = 0;
for (int u = s; par[u]; u = par[u])
tot += deg[u];
if (tot > mid) return 0;
for (int u = s, cnt = 0, cur = 1; par[u]; u = par[u], cur ++) {
int ign = 0;
for (int v : adj[u]) if (!in_path[v]) {
ign += tot + dp[v] <= mid;
cnt += tot + dp[v] > mid;
}
tot -= ign;
if (cnt > cur) return 0;
}
return 1;
}
void Hollwo_Pelw(){
cin >> n >> t >> s;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
find_path(t, 0);
for (int u = s; u; u = par[u])
in_path[u] = 1;
for (int u = s; par[u]; u = par[u]) {
for (int v : adj[u]) if (!in_path[v]) {
deg[u] ++;
calc_dp(v, u);
}
}
int l = 0, r = 2 * n;
while (l < r) {
int m = (l + r) >> 1;
if (check(m))
r = m;
else
l = m + 1;
}
cout << l;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |