Submission #487053

#TimeUsernameProblemLanguageResultExecution timeMemory
487053inksamuraiPapričice (COCI20_papricice)C++17
50 / 110
86 ms23064 KiB
#include <bits/stdc++.h> #define fi first #define se second #define pb push_back #define sz(a) (int)a.size() #define all(a) a.begin(),a.end() #define rep(i,n) for(int i=0;i<n;i++) #define crep(i,x,n) for(int i=x;i<n;i++) #define drep(i,n) for(int i=n-1;i>=0;i--) #define vec(...) vector<__VA_ARGS__> #define _322ZVFH ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) using namespace std; typedef long long ll; typedef long double ld; using pii=pair<int,int>; using vi=vector<int>; int main(){ _322ZVFH; int n; cin>>n; vec(vi) adj(n); rep(i,n-1){ int u,v; cin>>u>>v; u--,v--; adj[u].pb(v); adj[v].pb(u); } assert(n<=2000); vi chs(n,0); auto dfs=[&](auto self,int v,int par)->void{ chs[v]=1; for(auto u : adj[v]){ if(u!=par){ self(self,u,v); chs[v]+=chs[u]; } } }; dfs(dfs,0,-1); multiset<int> up,lo; int ans=1e9; auto g=[&](int _a,int _b,int _c){ ans=min(ans,max({_a,_b,_c})-min({_a,_b,_c})); }; auto f=[&](int v){ int l=chs[v]; vi rbts={(n+l)/2,(n-l)/2}; rep(i,2){ int rabbit=rbts[i]; auto it=(i==0?up.lower_bound(rabbit):lo.lower_bound(rabbit)); auto it1=(i==0?up.lower_bound(rabbit):lo.lower_bound(rabbit)); auto it2=(i==0?up.lower_bound(rabbit):lo.lower_bound(rabbit)); vi candies; if((i==0?it!=up.end():it!=lo.end())){ candies.pb(*it); } if((i==0?it1!=up.end():it1!=lo.end())){ it1=next(it1); candies.pb(*it1); } if((i==0?it2!=up.begin():it2!=lo.begin())){ it2=prev(it2); candies.pb(*it2); } for(auto x : candies){ if(x==chs[0]) continue; if(i==0) g(x-l,l,n-x); else g(x,l,n-x-l); } } }; auto dfs1=[&](auto self,int v,int par)->void{ f(v); up.insert(chs[v]); for(auto u : adj[v]){ if(u!=par){ self(self,u,v); } } lo.insert(chs[v]); if(up.find(chs[v])!=up.end()) up.erase(up.find(chs[v])); }; dfs1(dfs1,0,-1); cout<<ans<<"\n"; // return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...