Submission #318340

#TimeUsernameProblemLanguageResultExecution timeMemory
318340nkftPapričice (COCI20_papricice)C++17
110 / 110
358 ms29412 KiB
#include "bits/stdc++.h" using namespace std; #define int long long #define mp make_pair #define pb push_back #define pf push_front #define fi first #define se second #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define nbits(x) __builtin_popcount(x) #define gcd(x, y) __gcd(x, y) #define SYNC_OFF ios::sync_with_stdio(0); cin.tie(0); typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vii; const int MOD = 1e9 + 7; // 998244353; const int N = 200100; const int INF = 1e15; // 1e18; const double PI = acos(-1); const int dx[] = {-1,0,1,0,-1,1,1,-1}; const int dy[] = {0,1,0,-1,1,1,-1,-1}; int powmod(int a, int b, int m = MOD) { int res = 1; a %= m; assert(b >= 0); for (; b; b>>=1) { if (b & 1) res = res*a % m; a = a*a % m; } return res; } int n; int sz[N]; vector<int> g[N]; multiset<int> A, B; int dfs(int x, int p) { sz[x] = 1; for (int y: g[x]) { if (y != p) { sz[x] += dfs(y, x); } } return sz[x]; } vector<int> cand(multiset<int>& S, int k) { vector<int> res; auto ub = S.upper_bound(k); if (ub != S.end()) { res.pb(*ub); } auto lb = S.lower_bound(k); if (lb != S.end()) { res.pb(*lb); } if (lb != S.begin()) { res.pb(*prev(lb)); } return res; } int solve(int x, int p) { int ans = INF; for (int szy: cand(A, (n + sz[x]) / 2)) { int res = max({sz[x], szy - sz[x], n - szy}); res -= min({sz[x], szy - sz[x], n - szy}); ans = min(ans, res); } for (int szy: cand(B, (n - sz[x]) / 2)) { int res = max({sz[x], szy, n - sz[x] - szy}); res -= min({sz[x], szy, n - sz[x] - szy}); ans = min(ans, res); } A.insert(sz[x]); for (int y: g[x]) { if (y != p) { ans = min(ans, solve(y, x)); } } A.erase(A.find(sz[x])); B.insert(sz[x]); return ans; } signed main() { SYNC_OFF; cin >> n; for (int i = 1; i <= n - 1; i++) { int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } dfs(1, 0); cout << solve(1, 0) << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...