Submission #494999

# Submission time Handle Problem Language Result Execution time Memory
494999 2021-12-17T20:01:40 Z Victor Papričice (COCI20_papricice) C++17
110 / 110
751 ms 34376 KB
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define trav(a, x) for (auto &a : x)

#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define debug(x) cout << #x << " = " << x << endl

#define umap unordered_map
#define uset unordered_set

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;

const int INF = 1'000'000'007;

int n, ans = INF, sub_size[200001], root;
set<int> nodes[200001];
vi graph[200001];

void dfs(int u, int p) {
    sub_size[u] = 1;
    trav(v, graph[u]) if (v != p) {
        dfs(v, u);
        sub_size[u] += sub_size[v];
        if (sz(nodes[u]) < sz(nodes[v])) nodes[u].swap(nodes[v]);
        trav(val, nodes[v]) nodes[u].insert(val);
        nodes[v].clear();
    }

    trav(v, graph[u]) if (v != p) nodes[u].insert(sub_size[v]);

    if (u != root && sub_size[u] != 1) {
        int above = n - sub_size[u];

        auto it = nodes[u].lower_bound(sub_size[u] / 2);
        if (it == nodes[u].end()) --it;

        {
            int p1 = *it, p2 = sub_size[u] - p1;
            int mx = max(above, max(p1, p2)), mn = min(above, min(p1, p2));

            ans = min(ans, mx - mn);
        }

        if (it != nodes[u].begin()) --it;

        {
            int p1 = *it, p2 = sub_size[u] - p1;
            int mx = max(above, max(p1, p2)), mn = min(above, min(p1, p2));

            ans = min(ans, mx - mn);
        }
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    cin >> n;
    rep(i, 1, n) {
        int u, v;
        cin >> u >> v;
        graph[--u].pb(--v);
        graph[v].pb(u);
    }

    rep(i, 0, 10) {
        root = rand()%n;
        dfs(root, root);
        nodes[root].clear();
    }

    cout << ans << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 7 ms 14412 KB Output is correct
4 Correct 7 ms 14392 KB Output is correct
5 Correct 7 ms 14356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 7 ms 14412 KB Output is correct
4 Correct 7 ms 14392 KB Output is correct
5 Correct 7 ms 14356 KB Output is correct
6 Correct 10 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 9 ms 14496 KB Output is correct
9 Correct 9 ms 14468 KB Output is correct
10 Correct 10 ms 14412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 7 ms 14412 KB Output is correct
4 Correct 7 ms 14392 KB Output is correct
5 Correct 7 ms 14356 KB Output is correct
6 Correct 10 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 9 ms 14496 KB Output is correct
9 Correct 9 ms 14468 KB Output is correct
10 Correct 10 ms 14412 KB Output is correct
11 Correct 612 ms 21744 KB Output is correct
12 Correct 751 ms 24276 KB Output is correct
13 Correct 426 ms 24496 KB Output is correct
14 Correct 539 ms 24412 KB Output is correct
15 Correct 673 ms 24212 KB Output is correct
16 Correct 250 ms 23976 KB Output is correct
17 Correct 586 ms 24336 KB Output is correct
18 Correct 633 ms 34376 KB Output is correct
19 Correct 493 ms 24276 KB Output is correct
20 Correct 613 ms 24260 KB Output is correct