Submission #870214

#TimeUsernameProblemLanguageResultExecution timeMemory
870214NotLinuxPapričice (COCI20_papricice)C++17
110 / 110
226 ms31076 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 2e5 + 7; int n,ans = 1e18 + 7,subt[N]; vector < int > tree[N]; multiset < int > path , notpath; inline int eval(int a , int b , int c){ return max({abs(a-b) , abs(a-c) , abs(b-c)}); } inline vector < int > closest_set(int tar , multiset < int > &ste){ vector < int > ret; auto hi = ste.lower_bound(tar); auto lo = --ste.lower_bound(tar); if(hi != ste.end())ret.push_back(*hi); if(hi != ste.begin())ret.push_back(*lo); return ret; } inline void dfs0(int node , int past){ subt[node] = 1; for(auto itr : tree[node]){ if(itr != past){ dfs0(itr,node); subt[node] += subt[itr]; } } } inline void dfs1(int node , int past){ if(path.size()){ for(auto cand : closest_set((n+subt[node])/2 , path)){ ans = min(ans , eval(n-cand,cand-subt[node],subt[node])); } } if(notpath.size()){ for(auto cand : closest_set((n-subt[node])/2 , notpath)){ ans = min(ans , eval(n-cand-subt[node],cand,subt[node])); } } path.insert(subt[node]); for(auto itr : tree[node]){ if(itr != past){ dfs1(itr,node); } } path.erase(path.find(subt[node])); notpath.insert(subt[node]); } void solve(){ cin >> n; for(int i = 1;i<n;i++){ int u,v;cin >> u >> v; tree[u].push_back(v); tree[v].push_back(u); } //precalc : subtree size dfs0(1,0); dfs1(1,0); cout << ans << endl; } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0); int testcase = 1;//cin >> testcase; while(testcase--)solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...