Submission #464393

# Submission time Handle Problem Language Result Execution time Memory
464393 2021-08-13T06:42:24 Z abc864197532 Papričice (COCI20_papricice) C++17
110 / 110
510 ms 87664 KB
/*
 *   ##   #####   ####      ####  #    #  ####
 *  #  #  #    # #    #    #    # #    # #    #
 * #    # #####  #         #    # #    # #    #
 * ###### #    # #         #    # # ## # #    #
 * #    # #    # #    #    #    # ##  ## #    #
 * #    # #####   ####      ####  #    #  ####
 */
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define info() cerr << __PRETTY_FUNCTION__ << ": " << __LINE__ << endl
void abc() {cerr << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cerr << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.X >> a.Y;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.X << ", " << a.Y << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}
template <typename T> struct vv : vector <vector <T>> {
    vv(int n, int m, T v) : vector <vector <T>> (n, vector <T>(m, v)) {}
    vv() {}
};
template <typename T> struct vvv : vector <vv <T>> {
    vvv(int n, int m, int k, T v) : vector <vv <T>> (n, vv <T>(m, k, v)) {}
    vvv() {}
};
#ifdef Doludu
#define test(args...) info(), abc("[" + string(#args) + "]", args)
#define owo void(0)
#else
#define test(args...) void(0)
#define owo ios::sync_with_stdio(false); cin.tie(0)
#endif
const int mod = 1e9 + 7, N = 200001, logN = 18, K = 1000;

vector <int> adj[N];
int sz[N];

void dfs_sz(int v, int pa) {
    sz[v] = 1;
    for (int u : adj[v]) if (u != pa) {
        dfs_sz(u, v);
        sz[v] += sz[u];
    }
}

int ans = 1 << 30, n;

void calc(int a, int b, int c) {
    assert(a && b && c);
    ans = min(ans, max({a, b, c}) - min({a, b, c}));
}

multiset <int> pq[N];

void dfs_ans(int v, int pa) {
    for (int u : adj[v]) if (u != pa) {
        dfs_ans(u, v);
        if (pq[v].size() < pq[u].size()) swap(pq[v], pq[u]);
        for (int i : pq[u]) {
            auto it = pq[v].lower_bound((n - i + 1) / 2);
            if (it != pq[v].end()) calc(i, *it, n - i - *it);
            if (it != pq[v].begin()) --it, calc(i, *it, n - i - *it);
        }
        for (int i : pq[u]) pq[v].insert(i);
    }
    if (~pa) {
        auto it = pq[v].lower_bound((sz[v] + 1) / 2);
        if (it != pq[v].end()) calc(n - sz[v], *it, sz[v] - *it);
        if (it != pq[v].begin()) --it, calc(n - sz[v], *it, sz[v] - *it);
        pq[v].insert(sz[v]);
    }
}

int main () {
    owo;
    cin >> n;
    for (int i = 0, u, v; i < n - 1; ++i) {
        cin >> u >> v, --u, --v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs_sz(0, -1);
    dfs_ans(0, -1);
    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14412 KB Output is correct
2 Correct 10 ms 14408 KB Output is correct
3 Correct 10 ms 14412 KB Output is correct
4 Correct 10 ms 14336 KB Output is correct
5 Correct 10 ms 14412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14412 KB Output is correct
2 Correct 10 ms 14408 KB Output is correct
3 Correct 10 ms 14412 KB Output is correct
4 Correct 10 ms 14336 KB Output is correct
5 Correct 10 ms 14412 KB Output is correct
6 Correct 12 ms 14808 KB Output is correct
7 Correct 11 ms 14804 KB Output is correct
8 Correct 12 ms 14808 KB Output is correct
9 Correct 12 ms 14840 KB Output is correct
10 Correct 13 ms 14776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14412 KB Output is correct
2 Correct 10 ms 14408 KB Output is correct
3 Correct 10 ms 14412 KB Output is correct
4 Correct 10 ms 14336 KB Output is correct
5 Correct 10 ms 14412 KB Output is correct
6 Correct 12 ms 14808 KB Output is correct
7 Correct 11 ms 14804 KB Output is correct
8 Correct 12 ms 14808 KB Output is correct
9 Correct 12 ms 14840 KB Output is correct
10 Correct 13 ms 14776 KB Output is correct
11 Correct 463 ms 79428 KB Output is correct
12 Correct 473 ms 78888 KB Output is correct
13 Correct 402 ms 72956 KB Output is correct
14 Correct 462 ms 80812 KB Output is correct
15 Correct 510 ms 82876 KB Output is correct
16 Correct 330 ms 50120 KB Output is correct
17 Correct 452 ms 75492 KB Output is correct
18 Correct 281 ms 50536 KB Output is correct
19 Correct 481 ms 87664 KB Output is correct
20 Correct 430 ms 75076 KB Output is correct