답안 #376686

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
376686 2021-03-12T01:49:00 Z lyc Papričice (COCI20_papricice) C++14
110 / 110
316 ms 28396 KB
#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)

const int mxN = 2e5+5;

int N;
vector<int> al[mxN];
int sub[mxN];
multiset<int> done, vis;
int ans;

int subtree(int u, int p) {
    sub[u] = 1;
    for (int& v : al[u]) if (v != p) {
        sub[u] += subtree(v,u);
    }
    return sub[u];
}

int calc(int a, int b, int c) {
    return max({a,b,c}) - min({a,b,c});
}

void dfs(int u, int p) {
    multiset<int>::iterator it;
    // done
    it = done.lower_bound((N-sub[u]+1)/2);
    if (it != done.end()) {
        ans = min(ans, calc(N-*it-sub[u],*it,sub[u]));
    }
    if (it != done.begin()) {
        --it;
        ans = min(ans, calc(N-*it-sub[u],*it,sub[u]));
    }
    // vis
    it = vis.lower_bound((N-sub[u]+1)/2+sub[u]);
    if (it != vis.end()) {
        ans = min(ans, calc(N-*it,*it-sub[u],sub[u]));
    }
    if (it != vis.begin()) {
        --it;
        ans = min(ans, calc(N-*it,*it-sub[u],sub[u]));
    }

    vis.insert(sub[u]);
    for (int& v : al[u]) if (v != p)
        dfs(v,u);

    vis.erase(vis.find(sub[u]));
    done.insert(sub[u]);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    cin >> N;
    FOR(i,1,N-1){
        int X, Y; cin >> X >> Y;
        al[X].push_back(Y);
        al[Y].push_back(X);
    }

    ans = N;
    subtree(1,0);
    dfs(1,0);
    cout << ans << '\n';
}

# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 5 ms 5100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 5 ms 5100 KB Output is correct
6 Correct 5 ms 5228 KB Output is correct
7 Correct 6 ms 5228 KB Output is correct
8 Correct 5 ms 5228 KB Output is correct
9 Correct 5 ms 5228 KB Output is correct
10 Correct 5 ms 5228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 5 ms 5100 KB Output is correct
6 Correct 5 ms 5228 KB Output is correct
7 Correct 6 ms 5228 KB Output is correct
8 Correct 5 ms 5228 KB Output is correct
9 Correct 5 ms 5228 KB Output is correct
10 Correct 5 ms 5228 KB Output is correct
11 Correct 295 ms 24172 KB Output is correct
12 Correct 316 ms 24044 KB Output is correct
13 Correct 249 ms 24556 KB Output is correct
14 Correct 264 ms 24300 KB Output is correct
15 Correct 307 ms 24044 KB Output is correct
16 Correct 222 ms 24168 KB Output is correct
17 Correct 274 ms 24172 KB Output is correct
18 Correct 284 ms 28396 KB Output is correct
19 Correct 272 ms 24276 KB Output is correct
20 Correct 296 ms 24172 KB Output is correct