/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 2002;
int n;
vector <int> edges[N_MAX];
int sub[N_MAX];
bool anc[N_MAX][N_MAX];
void dfsAnc (int root, int u, int parent = -1)
{
anc[root][u] = true;
for(int v : edges[u])
if(v != parent)
dfsAnc(root, v, u);
}
void dfs (int u, int parent = -1)
{
sub[u] = 1;
dfsAnc(u, u, parent);
for(int v : edges[u])
if(v != parent)
{
dfs(v, u);
sub[u] += sub[v];
}
}
int f (int a, int b, int c)
{
return max({a, b, c}) - min({a, b, c});
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
edges[u].push_back(v);
edges[v].push_back(u);
}
dfs(1);
int ans = INT_MAX;
for(int i = 1; i <= n; i++)
for(int j = i + 1; j <= n; j++)
{
if(anc[i][j] == true)
ans = min(ans, f(sub[j], sub[i] - sub[j], n - sub[i]));
else if(anc[j][i] == true)
ans = min(ans, f(sub[i], sub[j] - sub[i], n - sub[j]));
else
ans = min(ans, f(sub[i], sub[j], n - sub[i] - sub[j]));
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
15 ms |
4332 KB |
Output is correct |
7 |
Correct |
18 ms |
4332 KB |
Output is correct |
8 |
Correct |
13 ms |
4332 KB |
Output is correct |
9 |
Correct |
14 ms |
4332 KB |
Output is correct |
10 |
Correct |
17 ms |
4332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
15 ms |
4332 KB |
Output is correct |
7 |
Correct |
18 ms |
4332 KB |
Output is correct |
8 |
Correct |
13 ms |
4332 KB |
Output is correct |
9 |
Correct |
14 ms |
4332 KB |
Output is correct |
10 |
Correct |
17 ms |
4332 KB |
Output is correct |
11 |
Runtime error |
1 ms |
620 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Halted |
0 ms |
0 KB |
- |