Submission #481208

#TimeUsernameProblemLanguageResultExecution timeMemory
481208CSQ31Papričice (COCI20_papricice)C++17
50 / 110
102 ms27108 KiB
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define sz(a) (int)(a.size()) #define all(a) a.begin(),a.end() #define lb lower_bound #define ub upper_bound #define owo ios_base::sync_with_stdio(0);cin.tie(0); #define MOD (ll)(998244353) #define INF (ll)(1e18) #define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr) #define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\ debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC)) typedef long long int ll; typedef long double ld; typedef pair<ll,ll> PII; typedef pair<int,int> pii; typedef vector<vector<int>> vii; typedef vector<vector<ll>> VII; ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);} const int MAXN = 2e5; int ans = 1e9,n; vii adj(MAXN); int sub[MAXN]; multiset<int>a,b; void dfs0(int v,int u){ sub[v] = 1; for(int x:adj[v]){ if(x==u)continue; dfs0(x,v); sub[v]+=sub[x]; } } void dfs(int v,int u){ //sub[i] ,sub[j] and n-sub[i]-sub[j] //we want sub[j] and n-sub[i]-sub[j] to be as close as possible //sub[j] = n-sub[i]-sub[j] //sub[j] = n-sub[i]/2; auto it = a.lb((n-sub[v])/2); if(it != a.end())ans = min(ans,max({sub[v],*it,n-sub[v]-*it}) - min({sub[v],*it,n-sub[v]-*it})); if(it != a.begin()){ it--; ans = min(ans,max({sub[v],*it,n-sub[v]-*it}) - min({sub[v],*it,n-sub[v]-*it})); } //ancestor //sub[i] , n-sub[j] , sub[j] - sub[i]; //n - sub[j] = sub[j] - sub[i] //sub[j] = (n+sub[i])/2 it = b.lb((n+sub[v])/2); if(it != b.end())ans = min(ans,max({sub[v],*it-sub[v],n-*it}) - min({sub[v],*it-sub[v],n-*it})); if(it != b.begin()){ it--; ans = min(ans,max({sub[v],*it-sub[v],n-*it}) - min({sub[v],*it-sub[v],n-*it})); } b.insert(sub[v]); for(int x:adj[v]){ if(x==u)continue; dfs(x,v); } b.erase(b.find(sub[v])); a.insert(sub[v]); } int main() { owo cin>>n; for(int i=0;i<n-1;i++){ int v,u; cin>>v>>u; adj[v].pb(u); adj[u].pb(v); } dfs0(1,0); dfs(1,0); cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...