Submission #511641

#TimeUsernameProblemLanguageResultExecution timeMemory
511641KoDPapričice (COCI20_papricice)C++17
50 / 110
1098 ms17296 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...