#pragma GCC optimize("O2")
#include <bits/stdc++.h>
#ifdef DEBUG
#include "debug.hpp"
#endif
using namespace std;
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define traverse(c, it) for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N) for(int i = 0; i < (N); ++i)
#define rrep(i, N) for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N) for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e) for(int i = (s); i <= (e); ++i)
#define rep3(i, s, e, d) for(int i = (s); (d) >= 0 ? i <= (e) : i >= (e); i += (d))
#define pb push_back
#ifdef DEBUG
#define debug(x...) { ++dbg::depth; \
string dbg_vals = dbg::to_string(x); \
--dbg::depth; \
dbg::fprint(__func__, __LINE__, #x, dbg_vals); }
#define light_debug(x) { dbg::light = 1; \
dbg::dout << __func__ << ":" << __LINE__ << " " \
<< #x << " = " << x << endl; \
dbg::light = 0; }
#else
#define debug(x...) 42
#define light_debug(x) 42
#endif
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }
template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef DEBUG
freopen("debug", "w", stderr);
#endif
int n, a, b; cin >> n >> a >> b; --a, --b;
vector<vector<int>> adj(n);
rep(_, n - 1) {
int u, v; cin >> u >> v; --u, --v;
adj[u].pb(v), adj[v].pb(u);
}
debug(adj);
auto solve_for = [&](int r) {
return y_combinator([&](auto rec, int u, int p) -> int {
vector<int> s; s.reserve(adj[u].size());
for(int v : adj[u])
if(v != p)
s.pb(rec(v, u));
sort(rall(s));
int res = 0;
rep(i, int(s.size()))
ckmax(res, s[i] += i + 1);
return res;
})(r, -1);
};
auto get_path = [&](int s, int e) {
vector<int> parent(n);
y_combinator([&](auto dfs, int u, int p) -> void {
parent[u] = p;
for(int v : adj[u])
if(v != p)
dfs(v, u);
})(s, -1);
vector<int> path;
for(int t = e; ~t; t = parent[t])
path.pb(t);
reverse(all(path));
return path;
};
vector<int> path = get_path(a, b);
vector<int> ans(int(path.size()) - 1, 1 << 27);
debug(path);
auto check = [&](int i) {
assert(i >= 0 && i + 1 < int(path.size()));
int x = path[i], y = path[i + 1];
adj[x].erase(find(all(adj[x]), y));
adj[y].erase(find(all(adj[y]), x));
int t_a = solve_for(a), t_b = solve_for(b);
ans[i] = max(t_a, t_b);
adj[x].pb(y);
adj[y].pb(x);
return t_a - t_b;
};
int j{-1};
for(int k = 1 << __lg(int(path.size())); k > 0; k >>= 1)
if(int i = j + k; i + 1 < int(path.size()) && check(i) < 0)
j = i;
if(j + 1 < int(path.size()))
check(j + 1);
cout << *min_element(all(ans)) << '\n';
#ifdef DEBUG
dbg::dout << "\nExecution time: " << clock() << "ms\n";
#endif
return 0;
}
Compilation message
torrent.cpp: In function 'int main()':
torrent.cpp:32:29: warning: statement has no effect [-Wunused-value]
32 | #define debug(x...) 42
| ^~
torrent.cpp:81:5: note: in expansion of macro 'debug'
81 | debug(adj);
| ^~~~~
torrent.cpp:32:29: warning: statement has no effect [-Wunused-value]
32 | #define debug(x...) 42
| ^~
torrent.cpp:116:5: note: in expansion of macro 'debug'
116 | debug(path);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
732 ms |
21592 KB |
Output is correct |
2 |
Correct |
732 ms |
23512 KB |
Output is correct |
3 |
Correct |
629 ms |
24864 KB |
Output is correct |
4 |
Correct |
665 ms |
24660 KB |
Output is correct |
5 |
Correct |
592 ms |
21204 KB |
Output is correct |
6 |
Correct |
456 ms |
21940 KB |
Output is correct |
7 |
Correct |
443 ms |
25428 KB |
Output is correct |