Submission #956048

# Submission time Handle Problem Language Result Execution time Memory
956048 2024-03-31T21:36:39 Z shezitt Papričice (COCI20_papricice) C++17
110 / 110
161 ms 23720 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];

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

vi closest(int x, set<int> &A){
    vi res;
    auto it = A.lower_bound(x);
    if(it != A.end()){
        res.pb(*it);
    }
    if(it != A.begin()){
        it--;
        res.pb(*it);
    }
    return res;
}

int balance(int x, int y, int z){
    return max({x, y, z}) - min({x, y, z});
}

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
    for(int p : closest((n + tam[x]) / 2, A)){
        res = min(res, balance(tam[x], p-tam[x], n-p));
    }

    // segundo caso
    for(int p : closest((n - tam[x]) / 2, B)){
        res = min(res, balance(tam[x], p, n-tam[x]-p));
    }

    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;

    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;

}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 3 ms 6492 KB Output is correct
7 Correct 3 ms 6580 KB Output is correct
8 Correct 2 ms 6488 KB Output is correct
9 Correct 3 ms 6492 KB Output is correct
10 Correct 3 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 3 ms 6492 KB Output is correct
7 Correct 3 ms 6580 KB Output is correct
8 Correct 2 ms 6488 KB Output is correct
9 Correct 3 ms 6492 KB Output is correct
10 Correct 3 ms 6492 KB Output is correct
11 Correct 112 ms 13976 KB Output is correct
12 Correct 130 ms 14060 KB Output is correct
13 Correct 95 ms 14912 KB Output is correct
14 Correct 124 ms 14788 KB Output is correct
15 Correct 161 ms 13904 KB Output is correct
16 Correct 74 ms 14532 KB Output is correct
17 Correct 117 ms 14216 KB Output is correct
18 Correct 143 ms 23720 KB Output is correct
19 Correct 159 ms 14536 KB Output is correct
20 Correct 118 ms 14188 KB Output is correct