#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
98 ms |
460 KB |
Output is correct |
7 |
Correct |
95 ms |
460 KB |
Output is correct |
8 |
Correct |
62 ms |
460 KB |
Output is correct |
9 |
Correct |
70 ms |
460 KB |
Output is correct |
10 |
Correct |
91 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
98 ms |
460 KB |
Output is correct |
7 |
Correct |
95 ms |
460 KB |
Output is correct |
8 |
Correct |
62 ms |
460 KB |
Output is correct |
9 |
Correct |
70 ms |
460 KB |
Output is correct |
10 |
Correct |
91 ms |
460 KB |
Output is correct |
11 |
Execution timed out |
1098 ms |
17296 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |