This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define mp make_pair
#define pb push_back
#define se second
#define fi first
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
ll set_on(ll n, ll k){
return (n |= 1 << k);
}
ll set_off(ll n, ll k){
return (n &= ~(1UL << k));
}
bool check_bit(ll n, ll k){
int bit = (n >> k) & 1U;
if(bit == 1)
return true;
return false;
}
int n, ans = 1e9;
vi G[200100], sz;
void dfs(int at, int prev){
sz[at] = 1;
for(auto next : G[at]){
if(next == prev) continue;
dfs(next, at);
sz[at] += sz[next];
}
}
set<int> A, B;
int opt(int a, int b, int c){
return max({a, b, c}) - min({a, b, c});
}
vi close(set<int>&s, int x){
vi v;
auto it = s.lower_bound(x);
if(it != s.end())v.pb(*it);
if(it != s.begin()){
--it;
v.pb(*it);
}
return v;
}
void dfs2(int at, int prev){
for(int szA : close(A, (n + sz[at]) / 2))
ans = min(ans, opt(sz[at], szA - sz[at], n - szA));
for(int szB : close(B, (n - sz[at]) / 2))
ans = min(ans, opt(sz[at], szB, n - sz[at] - szB));
A.insert(sz[at]);
for(auto next : G[at])
if(next != prev)
dfs2(next, at);
A.erase(sz[at]);
B.insert(sz[at]);
}
void solve(){
cin>>n;
sz.resize(n);
vector<pii> edge;
for(int i = 1; i < n; i++){
int a, b;
cin>>a>>b;
a--; b--;
G[a].pb(b);
G[b].pb(a);
}
dfs(0, -1);
dfs2(0, -1);
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout<<fixed;
startTime = clock();
int t=1;
// cin>>t;
while(t--)
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |