#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 : xlist) {
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 |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Incorrect |
94 ms |
460 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Incorrect |
94 ms |
460 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |