답안 #956047

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
956047 2024-03-31T21:28:57 Z shezitt Papričice (COCI20_papricice) C++17
110 / 110
143 ms 25856 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cassert>

using namespace std;

using ll = long long;

#define int ll
#define fore(i, a, b) for(int i=a; i<b; ++i)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pb(x) push_back(x)
#define vi vector<int>

const int N = 2e5+5;

vi adj[N];

int n;

int tam[N];
int pa[N];

void dfs(int x, int prev=-1){
    tam[x] = 1;
    pa[x] = prev;
    for(int child : adj[x]){
        if(child == prev) continue;
        dfs(child, x);
        tam[x] += tam[child];
    }
}

int f(int x, set<int> &A, set<int> &B, int pre=-1){

    // A -> Es ancestro
    // B -> No es ancestro
    int res = 1e9;

    // primer caso
    if(A.lower_bound((n+tam[x])/2) != A.end()){
        int p = *A.lower_bound((n + tam[x]) / 2);
        res = min(res, max({tam[x], p-tam[x], n-p}) - min({tam[x], p-tam[x], n-p}));
    }
    if(A.lower_bound((n + tam[x]) / 2) != A.begin()){
        int p = *prev(A.lower_bound((n + tam[x])/2));
        res = min(res, max({tam[x], p-tam[x], n-p}) - min({tam[x], p-tam[x], n-p}));
    }

    // segundo caso
    if(B.lower_bound((n - tam[x])/2) != B.end()){
        int q = *B.lower_bound((n - tam[x]) / 2);
        res = min(res, max({tam[x], q, n-tam[x]-q}) - min({tam[x], q, n-tam[x]-q}));
    }
    if(B.lower_bound((n - tam[x])/2) != B.begin()){
        int q = *prev(B.lower_bound((n - tam[x]) / 2));
        res = min(res, max({tam[x], q, n-tam[x]-q}) - min({tam[x], q, n-tam[x]-q}));
    }

    A.insert(tam[x]);
    for(int child : adj[x]){
        if(child == pre) continue;
        res = min(res, f(child, A, B, x));
    }

    A.erase(tam[x]);
    B.insert(tam[x]);

    return res;

}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n;

    // complete task

    fore(i, 0, n-1){
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    dfs(1);

    set<int> A, B;

    int ans = f(1, A, B, -1);

    cout << ans;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8028 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 3 ms 8028 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8028 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 3 ms 8028 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8120 KB Output is correct
6 Correct 3 ms 8028 KB Output is correct
7 Correct 3 ms 8028 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 2 ms 8028 KB Output is correct
10 Correct 3 ms 8028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8028 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 3 ms 8028 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8120 KB Output is correct
6 Correct 3 ms 8028 KB Output is correct
7 Correct 3 ms 8028 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 2 ms 8028 KB Output is correct
10 Correct 3 ms 8028 KB Output is correct
11 Correct 110 ms 18004 KB Output is correct
12 Correct 129 ms 18104 KB Output is correct
13 Correct 90 ms 18752 KB Output is correct
14 Correct 90 ms 18452 KB Output is correct
15 Correct 112 ms 18000 KB Output is correct
16 Correct 64 ms 17860 KB Output is correct
17 Correct 111 ms 18232 KB Output is correct
18 Correct 143 ms 25856 KB Output is correct
19 Correct 100 ms 18464 KB Output is correct
20 Correct 122 ms 18256 KB Output is correct