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>
using std::vector;
using std::array;
using std::pair;
using std::tuple;
template <class F> struct RecLambda : private F {
explicit RecLambda(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
vector<int> X(N), Y(N);
vector<vector<int>> graph(N);
for (int i = 0; i < N - 1; ++i) {
std::cin >> X[i] >> Y[i];
X[i] -= 1, Y[i] -= 1;
graph[X[i]].push_back(Y[i]);
graph[Y[i]].push_back(X[i]);
}
const auto dfs = RecLambda([&](auto&& dfs, const int u, const int p, vector<int>& list) -> int {
int s = 1;
for (const int v : graph[u]) {
if (v != p) {
s += dfs(v, u, list);
}
}
list.push_back(s);
return s;
});
int ans = N;
for (int i = 0; i < N - 1; ++i) {
vector<int> xlist, ylist;
const int xsize = dfs(X[i], Y[i], xlist);
const int ysize = dfs(Y[i], X[i], ylist);
for (const int x : xlist) {
const int y = ysize, z = xsize - x;
ans = std::min(ans, std::max({std::abs(x - y), std::abs(y - z), std::abs(z - x)}));
}
for (const int y : ylist) {
const int x = xsize, z = ysize - y;
ans = std::min(ans, std::max({std::abs(x - y), std::abs(y - z), std::abs(z - x)}));
}
}
std::cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |