답안 #366423

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
366423 2021-02-14T07:22:53 Z kartel Papričice (COCI20_papricice) C++14
50 / 110
1000 ms 33644 KB
#include <bits/stdc++.h>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
#define F first
#define S second
#define pb push_back
#define M ll(1e9 + 7)
#define sz(x) (int)x.size()
#define re return
#define oo ll(1e18)
#define el '\n'
#define pii pair <int, int>
#define all(x) (x).begin(), (x).end()
#define arr_all(x, n) (x + 1), (x + 1 + n)
#define vi vector<int>
#define eps (ld)1e-10
using namespace std;
typedef long long ll;
//using namespace __gnu_pbds;
//typedef tree <ll, null_type, less_equal <ll> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long double ld;
typedef unsigned long long ull;

const int N = 2e5 + 500;

set <int> vc[N];
int pr[N], siz[N], ans = 1e9;
vector <int> g[N];
int n;

int f(int v) { return (pr[v] == v ? v : pr[v] = f(pr[v])); }

void link(int v, int u) {
    v = f(v);
    u = f(u);

    if (sz(vc[v]) > sz(vc[u]))
        swap(u, v);

    set <int> :: iterator it = vc[u].begin();

    for (auto x : vc[v]) {
        while (it != vc[u].end() && *it < x)
            it++;

        if (it != vc[u].end()) {
            ans = min(ans, max({n - x - *it, *it, x}) - min({n - x - *it, *it, x}));
        }
    }

    for (auto x : vc[v])
        vc[u].insert(x);

    pr[v] = u;
}

void dfs(int v, int pred) {
    siz[v] = 1;
    pr[v] = v;

    for (auto u : g[v]) {
        if (u == pred)
            continue;

        dfs(u, v);

        siz[v] += siz[u];
        int U = f(u);

        set<int> ::iterator it = vc[U].lower_bound(siz[u] / 2);

        ans = min(ans, max({n - siz[u], siz[u] - *it, *it}) - min({n - siz[u], siz[u] - *it, *it}));
        if (it != vc[U].begin()) {
            it--;
            ans = min(ans, max({n - siz[u], siz[u] - *it, *it}) - min({n - siz[u], siz[u] - *it, *it}));
            it++;
        }

        if (it != vc[U].end() && ++it != vc[U].end()) {

            ans = min(ans, max({n - siz[u], siz[u] - *it, *it}) - min({n - siz[u], siz[u] - *it, *it}));
        }

        link(u, v);
    }

    vc[f(v)].insert(siz[v]);
}

int main()
{
//    mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());;
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//  in("pizza.in");
//  out("pizza.out");
//    in("input.txt");
////    out("output.txt");

    cin >> n;

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;

        g[u].pb(v);
        g[v].pb(u);
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++)
            vc[j].clear();
        dfs(i, -1);
    }

    cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 14444 KB Output is correct
2 Correct 16 ms 14444 KB Output is correct
3 Correct 14 ms 14444 KB Output is correct
4 Correct 15 ms 14572 KB Output is correct
5 Correct 16 ms 14444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 14444 KB Output is correct
2 Correct 16 ms 14444 KB Output is correct
3 Correct 14 ms 14444 KB Output is correct
4 Correct 15 ms 14572 KB Output is correct
5 Correct 16 ms 14444 KB Output is correct
6 Correct 774 ms 14828 KB Output is correct
7 Correct 940 ms 14728 KB Output is correct
8 Correct 540 ms 14700 KB Output is correct
9 Correct 648 ms 14828 KB Output is correct
10 Correct 899 ms 14828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 14444 KB Output is correct
2 Correct 16 ms 14444 KB Output is correct
3 Correct 14 ms 14444 KB Output is correct
4 Correct 15 ms 14572 KB Output is correct
5 Correct 16 ms 14444 KB Output is correct
6 Correct 774 ms 14828 KB Output is correct
7 Correct 940 ms 14728 KB Output is correct
8 Correct 540 ms 14700 KB Output is correct
9 Correct 648 ms 14828 KB Output is correct
10 Correct 899 ms 14828 KB Output is correct
11 Execution timed out 1101 ms 33644 KB Time limit exceeded
12 Halted 0 ms 0 KB -