Submission #482519

#TimeUsernameProblemLanguageResultExecution timeMemory
482519jalsolPapričice (COCI20_papricice)C++17
50 / 110
1067 ms17800 KiB
#ifdef LOCAL #include "local.h" #else #include <bits/stdc++.h> #define debug(...) #define DB(...) #endif using namespace std; using ll = long long; using ld = long double; #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define For(i, l, r) for (int i = (l); i <= (r); ++i) #define Ford(i, r, l) for (int i = (r); i >= (l); --i) #define Rep(i, n) For (i, 0, (n) - 1) #define Repd(i, n) Ford (i, (n) - 1, 0) #define Fe(...) for (auto __VA_ARGS__) template <class C> inline int isz(const C& c) { return static_cast<int>(c.size()); } template <class T> inline bool chmin(T& a, const T& b) { return (a > b) ? a = b, true : false; } template <class T> inline bool chmax(T& a, const T& b) { return (a < b) ? a = b, true : false; } const bool __initialization = []() { cin.tie(nullptr)->sync_with_stdio(false); #define TASK "empty" if (fopen(TASK".inp", "r")) { (void)(!freopen(TASK".inp", "r", stdin)); (void)(!freopen(TASK".out", "w", stdout)); } cout << setprecision(12) << fixed; return true; }(); constexpr ld eps = 1e-9; constexpr int inf = 1e9; constexpr ll linf = 1e18; // ============================================================================= constexpr int maxn = 2e5 + 5; struct Edge { int u, v; int other(int x) const { return u ^ v ^ x; } }; int n; vector<int> g[maxn]; Edge e[maxn]; int sz[maxn]; int color[maxn]; int par[maxn]; void Dfs(int u, int c) { sz[u] = 1; Fe (id : g[u]) { if (color[id]) continue; color[id] = c; int v = e[id].other(u); par[v] = u; Dfs(v, c); sz[u] += sz[v]; } } signed main() { cin >> n; For (i, 1, n - 1) { int u, v; cin >> u >> v; g[u].push_back(i); g[v].push_back(i); e[i] = { u, v }; } int ans = n; For (i, 1, n - 1) { auto [u, v] = e[i]; memset(color + 1, 0, 4 * (n - 1)); color[i] = -1; par[u] = -1; Dfs(u, 1); par[v] = -1; Dfs(v, 2); int subu = sz[u]; int subv = sz[v]; For (j, 1, n - 1) { if (i == j) continue; auto [pu, pv] = e[j]; if (pv == par[pu]) swap(pu, pv); array<int, 3> comp; if (color[j] == 1) { comp = { subv, sz[pv], subu - sz[pv] }; } else { comp = { subu, sz[pv], subv - sz[pv] }; } sort(all(comp)); int diff = comp[2] - comp[0]; chmin(ans, diff); } } cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...