Submission #956047

#TimeUsernameProblemLanguageResultExecution timeMemory
956047shezittPapričice (COCI20_papricice)C++17
110 / 110
143 ms25856 KiB
#include <iostream> #include <vector> #include <algorithm> #include <set> #include <cassert> using namespace std; using ll = long long; #define int ll #define fore(i, a, b) for(int i=a; i<b; ++i) #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define pb(x) push_back(x) #define vi vector<int> const int N = 2e5+5; vi adj[N]; int n; int tam[N]; int pa[N]; void dfs(int x, int prev=-1){ tam[x] = 1; pa[x] = prev; for(int child : adj[x]){ if(child == prev) continue; dfs(child, x); tam[x] += tam[child]; } } int f(int x, set<int> &A, set<int> &B, int pre=-1){ // A -> Es ancestro // B -> No es ancestro int res = 1e9; // primer caso if(A.lower_bound((n+tam[x])/2) != A.end()){ int p = *A.lower_bound((n + tam[x]) / 2); res = min(res, max({tam[x], p-tam[x], n-p}) - min({tam[x], p-tam[x], n-p})); } if(A.lower_bound((n + tam[x]) / 2) != A.begin()){ int p = *prev(A.lower_bound((n + tam[x])/2)); res = min(res, max({tam[x], p-tam[x], n-p}) - min({tam[x], p-tam[x], n-p})); } // segundo caso if(B.lower_bound((n - tam[x])/2) != B.end()){ int q = *B.lower_bound((n - tam[x]) / 2); res = min(res, max({tam[x], q, n-tam[x]-q}) - min({tam[x], q, n-tam[x]-q})); } if(B.lower_bound((n - tam[x])/2) != B.begin()){ int q = *prev(B.lower_bound((n - tam[x]) / 2)); res = min(res, max({tam[x], q, n-tam[x]-q}) - min({tam[x], q, n-tam[x]-q})); } A.insert(tam[x]); for(int child : adj[x]){ if(child == pre) continue; res = min(res, f(child, A, B, x)); } A.erase(tam[x]); B.insert(tam[x]); return res; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; // complete task fore(i, 0, n-1){ int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } dfs(1); set<int> A, B; int ans = f(1, A, B, -1); cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...