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>
using namespace std;
const int MAXN = 2e5 + 25;
vector <int> adj[MAXN];
int sze[MAXN], n, in[MAXN], out[MAXN], t;
typedef long long ll;
int mn = 1e9;
void dfs (int pos, int par) {
	sze[pos] = 1;
	in[pos] = ++t;
	int mx = 0;
	for (auto j : adj[pos]) {
		if (j == par) continue;
		dfs(j, pos);
		sze[pos] += sze[j]; mx = max(mx, sze[j]);
	}
	out[pos] = t;
	if (sze[pos] != 1) {
		int x = n - sze[pos];
		mn = min(mn, max({x, sze[pos] - mx, mx}) - min({x, sze[pos] - mx, mx}));
	}
}
bool check (int x, int y) {
	return in[y] < in[x] && in[x] <= out[y];
}
int main () {
	cin >> n;
	for (int i = 1; i < n; i++) {
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, -1);
	for (int i = 2; i <= n; i++) {
		for (int j = i + 1; j <= n; j++) {
			if (check(i, j) || check(j, i)) continue;
			int x = n - sze[j] - sze[i];
			mn = min(mn, max({sze[i], sze[j], x}) - min({sze[i], sze[j], x}));
		}
	}
	cout << mn << '\n';
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |