Submission #358337

# Submission time Handle Problem Language Result Execution time Memory
358337 2021-01-25T11:04:41 Z egekabas Papričice (COCI20_papricice) C++14
15 / 110
7 ms 5228 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
int n;
int calcval(int a, int b){
    int c = n-a-b;
    return max({abs(a-b), abs(a-c), abs(b-c)});
}
int ans = 1e9;
    
vector<int> g[200009];
multiset<int> s, ins;
int sz[200009];
int opt[200009];
int optin[200009];
void dfs1(int v, int prt){
    sz[v] = 1;
    for(auto u : g[v])
        if(u != prt){
            dfs1(u, v);
            sz[v] += sz[u];
        }
    s.insert(sz[v]);
}
void calc(int x, int opt, multiset<int> &s, int type){
    auto it = s.lower_bound(opt);
    if(it != s.end()){
        if(type)
            ans = min(ans, calcval(x-*it, *it));
        else    
            ans = min(ans, calcval(x, *it));
    }
    if(it != s.begin()){
        --it;
        if(type)
            ans = min(ans, calcval(x-*it, *it));
        else    
            ans = min(ans, calcval(x, *it));
    }
}
void clean(int v, int prt, int type){
    if(type == 1){
        s.insert(sz[v]);
        ins.erase(ins.lower_bound(sz[v]));
    }
    else{
        s.erase(s.lower_bound(sz[v]));
        ins.insert(sz[v]);
    }
    for(auto u : g[v])
        if(u != prt)
            clean(u, v, type);
}
void dfs2(int v, int prt, int del){
    pii maxi = {0, 0};
    for(auto u : g[v])
        if(u != prt)
            maxi = max(maxi, {sz[u], u});
    for(auto u : g[v])
        if(u != prt && u != maxi.ss)
            dfs2(u, v, 0);
    if(maxi.ss)
        dfs2(maxi.ss, v, 1);
    s.erase(s.lower_bound(sz[v]));
    ins.insert(sz[v]);
    calc(sz[v], opt[sz[v]], s, 0);
    calc(sz[v], n-sz[v]-opt[sz[v]], s, 0);
    calc(sz[v], optin[sz[v]], ins, 1);
    calc(sz[v], sz[v]-optin[sz[v]], ins, 1);
    
    if(del == 1){
        for(auto u : g[v])
            if(u != prt && u != maxi.ss)
                clean(u, v, 0);
    }
    else{
        s.insert(sz[v]);
        ins.erase(ins.lower_bound(sz[v]));
        if(maxi.ss)
            clean(maxi.ss, v, 1);
    }
}
    
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    cin >> n;
    for(int i = 0; i < n-1; ++i){
        int x, y;
        cin >> x >> y;
        g[x].pb(y);
        g[y].pb(x);
    }
    int cur = 1;
    for(int i = n; i >= 1; --i){
        while(cur != n-2 && calcval(i, cur) > calcval(i, cur+1))
            ++cur;
        opt[i] = cur;
    }
    for(int i = 1; i <= n; ++i){
        int l = 1, r = n-2;
        while(l < r){
            int m = (l+r)/2;
            if(calcval(m+1, i-(m+1)) < calcval(m, i-m))
                l = m+1;
            else
                r = m;
        }
        optin[i] = l;
    }
    dfs1(1, 0);
    dfs2(1, 0, 0);
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5100 KB Output is correct
2 Correct 5 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 4 ms 5008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5100 KB Output is correct
2 Correct 5 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 4 ms 5008 KB Output is correct
6 Correct 7 ms 5228 KB Output is correct
7 Incorrect 7 ms 5228 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5100 KB Output is correct
2 Correct 5 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 4 ms 5008 KB Output is correct
6 Correct 7 ms 5228 KB Output is correct
7 Incorrect 7 ms 5228 KB Output isn't correct
8 Halted 0 ms 0 KB -