Submission #342373

#TimeUsernameProblemLanguageResultExecution timeMemory
342373limabeansPapričice (COCI20_papricice)C++17
50 / 110
1097 ms29292 KiB
#include <bits/stdc++.h> using namespace std; template<typename T> void out(T x) { cout << x << endl; exit(0); } #define watch(x) cout << (#x) << " is " << (x) << endl const int maxn = 2e5 + 10; const int LOG = 20; int n; vector<int> g[maxn]; int par[LOG+1][maxn]; int dep[maxn]; int siz[maxn]; void hack(int at, int p=0) { for (int i=1; i<LOG; i++) { par[i][at] = par[i-1][par[i-1][at]]; } siz[at] = 1; for (int to: g[at]) { if (to==p) continue; dep[to]=1+dep[at]; par[0][to]=at; hack(to, at); siz[at] += siz[to]; } } int lca(int u, int v) { if (dep[u] < dep[v]) swap(u,v); int dx = dep[u]-dep[v]; for (int i=0; i<LOG; i++) { if (dx>>i&1) { u = par[i][u]; } } if (u==v) return u; for (int j=LOG-1; j>=0; j--) { if (par[j][u]!=par[j][v]) { u = par[j][u]; v = par[j][v]; } } return par[0][u]; } int get(vector<int> a) { sort(a.begin(),a.end()); return a[2]-a[0]; } int lo; set<int> A, B; void dfs(int at, int p) { for (int y: A) { lo = min(lo, get({siz[at],siz[y]-siz[at],n-siz[y]})); } for (int y: B) { lo = min(lo, get({siz[at],siz[y],n-siz[at]-siz[y]})); } A.insert(at); for (int to: g[at]) { if (to == p) continue; dfs(to, at); } A.erase(at); B.insert(at); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n; for (int i=0; i<n-1; i++) { int u,v; cin>>u>>v; g[u].push_back(v); g[v].push_back(u); } assert(n>=3); hack(1); lo = n; dfs(1,0); cout<<lo<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...