Submission #1121006

#TimeUsernameProblemLanguageResultExecution timeMemory
1121006steveonalexPapričice (COCI20_papricice)C++17
110 / 110
239 ms41684 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll gcd(ll a, ll b){return __gcd(a, b);} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ll mask){return __builtin_ctzll(mask);} int logOf(ll mask){return __lg(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T& container, string separator = " ", string finish = "\n"){ for(auto item: container) cout << item << separator; cout << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } const int N = 2e5 + 69; int n; vector<int> graph[N]; int sz[N]; set<int> S[N]; int ans = 1e9; void find_sz(int u, int p){ sz[u] = 1; for(int v: graph[u]) if (v != p){ find_sz(v, u); sz[u] += sz[v]; } } pair<int, int> get_upper_lower(set<int> &S, int x){ pair<int, int> range = {-1, -1}; auto it = S.lower_bound(x); if (it != S.end()) range.second = *it; if (it != S.begin()) { it--; range.first = *it; } return range; } void dfs(int u, int p){ for(int v: graph[u]) if (v != p){ dfs(v, u); if (S[u].size() < S[v].size()) swap(S[u], S[v]); for(int i: S[v]) { pair<int, int> range = get_upper_lower(S[u], (n - i) / 2); minimize(ans, max({range.first, n - range.first - i, i}) - min({range.first, n - range.first - i, i})); minimize(ans, max({range.second, n - range.second - i, i}) - min({range.second, n - range.second - i, i})); } for(int i: S[v]){ S[u].insert(i); } } if (!S[u].empty()){ pair<int, int> range = get_upper_lower(S[u], sz[u] / 2); minimize(ans, max({range.first, sz[u] - range.first, n - sz[u]}) - min({range.first, sz[u] - range.first, n - sz[u]})); minimize(ans, max({range.second, sz[u] - range.second, n - sz[u]}) - min({range.second, sz[u] - range.second, n - sz[u]})); } S[u].insert(sz[u]); } void solve(){ cin >> n; for(int i = 1; i<n; ++i) { int u, v; cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); } find_sz(1, 0); dfs(1, 0); cout << ans << "\n"; } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); clock_t start = clock(); solve(); cerr << "Time elapsed: " << clock() - start << "ms!\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...