#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5020 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
5 ms |
5100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5020 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
5 ms |
5100 KB |
Output is correct |
11 |
Correct |
184 ms |
12440 KB |
Output is correct |
12 |
Correct |
216 ms |
14760 KB |
Output is correct |
13 |
Correct |
152 ms |
15112 KB |
Output is correct |
14 |
Correct |
176 ms |
14880 KB |
Output is correct |
15 |
Correct |
173 ms |
14700 KB |
Output is correct |
16 |
Correct |
126 ms |
14740 KB |
Output is correct |
17 |
Correct |
183 ms |
14912 KB |
Output is correct |
18 |
Correct |
193 ms |
22480 KB |
Output is correct |
19 |
Correct |
205 ms |
14844 KB |
Output is correct |
20 |
Correct |
222 ms |
14876 KB |
Output is correct |