제출 #321772

#제출 시각아이디문제언어결과실행 시간메모리
321772alextodoranPapričice (COCI20_papricice)C++17
50 / 110
18 ms4332 KiB
/**
 ____ ____ ____ ____ ____
||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;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...