#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
36 ms |
492 KB |
Output is correct |
7 |
Correct |
36 ms |
492 KB |
Output is correct |
8 |
Correct |
41 ms |
620 KB |
Output is correct |
9 |
Correct |
28 ms |
492 KB |
Output is correct |
10 |
Correct |
36 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
36 ms |
492 KB |
Output is correct |
7 |
Correct |
36 ms |
492 KB |
Output is correct |
8 |
Correct |
41 ms |
620 KB |
Output is correct |
9 |
Correct |
28 ms |
492 KB |
Output is correct |
10 |
Correct |
36 ms |
492 KB |
Output is correct |
11 |
Execution timed out |
1098 ms |
16236 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |