답안 #879781

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
879781 2023-11-28T06:09:00 Z TAhmed33 Papričice (COCI20_papricice) C++
50 / 110
1000 ms 218320 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
const int MAXN = 2e5 + 25;
#define mid ((l + r) >> 1)
#define tl (node + 1)
#define tr (node + 2 * (mid - l + 1))
const int bad = -1e7;
int comb (int a, int b, int c) {
    return abs(2 * a - c) < abs(2 * b - c) ? a : b;
}
struct SegmentTree {
    multiset <int> tree[2 * MAXN];
    int arr[MAXN];
    void build (int l, int r, int node) {
        for (int i = l; i <= r; i++) tree[node].insert(arr[i]);
        if (l == r) return;
        build(l, mid, tl); build(mid + 1, r, tr);
    }
    void update (int l, int r, int a, int x, int y, int node) {
        if (l > a || r < a) return;
        tree[node].erase(tree[node].lower_bound(y));
        tree[node].insert(x);
        if (l == r) return;
        update(l, mid, a, x, y, tl); update(mid + 1, r, a, x, y, tr);
    }
    int get (int l, int r, int a, int b, int c, int node) {
        if (a > b || l > r) return bad;
        if (l > b || r < a) return bad;
        if (l >= a && r <= b) {
            int ret = bad;
            auto t = tree[node].lower_bound(c);
            if (t != tree[node].end()) ret = comb(ret, *t, c);
            if (t != tree[node].begin()) {
                t--;
                ret = comb(ret, *t, c);
            }
            return ret;
        }
        return comb(get(l, mid, a, b, c, tl), get(mid + 1, r, a, b, c, tr), c);
    }
} cur;
vector <int> adj[MAXN];
int sze[MAXN], n, in[MAXN], out[MAXN], t;
set <int> dd[MAXN];
typedef long long ll;
int mn = 1e9; 
void dfs (int pos, int par) {
	sze[pos] = 1;
	in[pos] = ++t;
    int mx = bad;
	for (auto j : adj[pos]) {
		if (j == par) continue;
		dfs(j, pos);
		sze[pos] += sze[j];
        if (dd[pos].size() < dd[j].size()) swap(dd[pos], dd[j]);
        for (auto i : dd[j]) dd[pos].insert(i);
        dd[j].clear();
	}
    for (auto i : dd[pos]) mx = comb(i, mx, sze[pos]);
    dd[pos].insert(sze[pos]);
    if (sze[pos] != 1 && pos != 1) mn = min(mn, max({n - sze[pos], sze[pos] - mx, mx}) - min({n - sze[pos], sze[pos] - mx, mx}));
	out[pos] = t;
}
void dfs2 (int pos, int par) {
    if (pos != 1) {
        int x = comb(cur.get(1, n, 1, in[pos] - 1, n - sze[pos], 1), cur.get(1, n, out[pos] + 1, n, n - sze[pos], 1), n - sze[pos]);
        mn = min(mn, max({sze[pos], x, n - sze[pos] - x}) - min({sze[pos], x, n - sze[pos] - x}));
    }
    if (pos != 1) cur.update(1, n, in[pos], bad, sze[pos], 1);
    for (auto j : adj[pos]) if (j != par) dfs2(j, pos);
    if (pos != 1) cur.update(1, n, in[pos], sze[pos], bad, 1);
}
int main () {
    ios::sync_with_stdio(0);
    cin.tie(0);
	cin >> n;
	for (int i = 1; i < n; i++) {
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, -1);
    for (int i = 2; i <= n; i++) {
        cur.arr[in[i]] = sze[i];
    }
    cur.arr[in[1]] = bad;
    cur.build(1, n, 1);
    dfs2(1, -1);
	cout << mn << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 35676 KB Output is correct
2 Correct 8 ms 35736 KB Output is correct
3 Correct 7 ms 35672 KB Output is correct
4 Correct 8 ms 35676 KB Output is correct
5 Correct 7 ms 35784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 35676 KB Output is correct
2 Correct 8 ms 35736 KB Output is correct
3 Correct 7 ms 35672 KB Output is correct
4 Correct 8 ms 35676 KB Output is correct
5 Correct 7 ms 35784 KB Output is correct
6 Correct 16 ms 36700 KB Output is correct
7 Correct 17 ms 36696 KB Output is correct
8 Correct 15 ms 36952 KB Output is correct
9 Correct 15 ms 36700 KB Output is correct
10 Correct 16 ms 36696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 35676 KB Output is correct
2 Correct 8 ms 35736 KB Output is correct
3 Correct 7 ms 35672 KB Output is correct
4 Correct 8 ms 35676 KB Output is correct
5 Correct 7 ms 35784 KB Output is correct
6 Correct 16 ms 36700 KB Output is correct
7 Correct 17 ms 36696 KB Output is correct
8 Correct 15 ms 36952 KB Output is correct
9 Correct 15 ms 36700 KB Output is correct
10 Correct 16 ms 36696 KB Output is correct
11 Execution timed out 1078 ms 218320 KB Time limit exceeded
12 Halted 0 ms 0 KB -