답안 #495670

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
495670 2021-12-19T20:49:35 Z AlperenT Papričice (COCI20_papricice) C++17
0 / 110
3 ms 4940 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5, INF = 1e9 + 5;

int n, a, b, cnt[N], ans = INF, x, y, z;

set<int> st;

vector<int> tree[N];

void dfs(int v, int p = 0){
    cnt[v] = 1;

    for(auto e : tree[v]){
        if(e != p){
            dfs(e, v);
            cnt[v] += cnt[e];
        }
    }
}

void solve(int v, int p = 0){
    for(auto e : tree[v]){
        if(e != p) solve(e, v);
    }

    st.insert(cnt[v]);

    x = n - cnt[v];

    auto it = st.upper_bound(cnt[v] / 2);

    if(it != st.end()){
        y = *it, z = cnt[v] - y;

        ans = min(ans, max({x, y, z}) - min({x, y, z}));
    }
    if(it != st.begin()){
        y = *prev(it), z = cnt[v] - y;

        ans = min(ans, max({x, y, z}) - min({x, y, z}));
    }
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);

    cin >> n;

    for(int i = 0; i < n - 1; i++){
        cin >> a >> b;

        tree[a].push_back(b);
        tree[b].push_back(a);
    }

    dfs(1);

    solve(1);

    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Incorrect 2 ms 4940 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Incorrect 2 ms 4940 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Incorrect 2 ms 4940 KB Output isn't correct
4 Halted 0 ms 0 KB -