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 pii pair<int, int>
#define mp make_pair
#define F first
#define S second
#define iter(a) a.begin(), a.end()
#define eb emplace_back
using namespace std;
typedef long long ll;
const ll MAX = 2147483647;
vector<vector<int>> g;
vector<int> sz, in, out;
int ts = 0;
void dfs(int now, int p){
in[now] = ts++;
for(int i : g[now]){
if(i == p) continue;
dfs(i, now);
sz[now] += sz[i];
}
out[now] = ts++;
}
bool isAnc(int a, int b){
return in[a] <= in[b] && out[a] >= out[b];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
g.resize(n + 1);
in.resize(n + 1);
out.resize(n + 1);
sz.resize(n + 1, 1);
for(int i = 0; i < n - 1; i ++){
int u, v;
cin >> u >> v;
g[u].eb(v);
g[v].eb(u);
}
dfs(1, 1);
int ans = n;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(isAnc(i, j) || isAnc(j, i)) continue;
int all = n - sz[i] - sz[j];
ans = min(ans, max({abs(sz[i] - sz[j]), abs(all - sz[j]), abs(all - sz[i])}));
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(!isAnc(i, j)) continue;
int t0 = n - sz[i];
int t1 = sz[i] - sz[j];
int t2 = sz[j];
ans = min(ans, max({abs(t0 - t1), abs(t1 - t2), abs(t2 - t0)}));
}
}
cout << ans << "\n";
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... |