Submission #914348

#TimeUsernameProblemLanguageResultExecution timeMemory
914348marvinthangBeech Tree (IOI23_beechtree)C++17
100 / 100
177 ms56984 KiB
/*************************************
*    author: marvinthang             *
*    created: 21.01.2024 19:34:55    *
*************************************/

#include "beechtree.h"
#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i-- > 0; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }

// end of template

vector <int> beechtree(int N, int M, vector <int> P, vector <int> C) {
    vector <vector <int>> adj(N);
    vector <int> sz(N, 1), res(N, 1), heavy(N, -1);
    vector <bool> is_head(N, true);
    REPD(u, N) {
        --C[u];
        if (heavy[u] != -1) is_head[heavy[u]] = false;
        if (u) {
            sz[P[u]] += sz[u];
            adj[P[u]].push_back(u);
            if (heavy[P[u]] == -1 || sz[heavy[P[u]]] < sz[u]) heavy[P[u]] = u;
        }
    }
    vector <pair <int, int>> val;
    vector <int> cnt(N + 1);
    REP(u, N) if (is_head[u]) {
        val.emplace_back(u, u);
        ++cnt[sz[u]];
        for (int i = (int) val.size() - 1; i < (int) val.size(); ++i) {
            for (int &v: adj[val[i].fi]) {
                ++cnt[sz[v]];
                val.emplace_back(v, u);
            }
        }
    }
    partial_sum(ALL(cnt), cnt.begin());
    vector <pair <int, int>> sorted_val(cnt.back());
    vector <vector <int>> order(N);
    for (auto &v: val) sorted_val[--cnt[sz[v.fi]]] = v;
    for (auto &[v, u]: sorted_val) order[u].push_back(v);

    vector <int> tin(N), tout(N);
    function<void(int)> dfs1 = [&] (int u) {
        static int t = 0;
        tin[u] = t++;
        for (int &v: adj[u]) dfs1(v);
            tout[u] = t;
    };
    dfs1(0);
    vector <int> color_sz(M, 0);

    REPD(u, N) {
        if (res[u]) {
            for (int &v: adj[u]) {
                if (color_sz[C[v]]) {
                    res[u] = 0;
                    break;
                }
                color_sz[C[v]] = 1;
            }
            for (int &v: adj[u]) color_sz[C[v]] = 0;
        }
        if (u && !res[u]) res[P[u]] = 0;
    }
    function<void(int)> dfs2 = [&] (int u) {
        auto check_subtree = [&] (int v) {
            int last = -1;
            for (int &w: order[u]) if (tin[v] <= tin[w] && tin[w] < tout[v]) {
                int cnt = (int) adj[last].size();
                for (int &v: adj[w]) {
                    if (color_sz[C[v]] > sz[v]) {
                        if (last != -1) for (int &v: adj[last]) color_sz[C[v]] = 0;
                        return true;
                    }
                    cnt -= !!color_sz[C[v]];
                }
                if (last != -1 && cnt) {
                    for (int &v: adj[last]) color_sz[C[v]] = 0;
                    return true;
                }
                for (int &v: adj[w]) color_sz[C[v]] = sz[v];
                last = w;
            }
            for (int &v: adj[last]) color_sz[C[v]] = 0;
            return false;
        };
        vector <int> path;
        for (int v = u; v != -1; v = heavy[v]) path.push_back(v);
        int tl = 0;
        while (tl < (int) path.size() && !res[path[tl]]) ++tl;
        auto p = partition_point(tl + ALL(path), check_subtree);
        if (p != path.end()) res[*p] = 1;
        for (auto it = path.begin(); it != p; ++it) {
            for (int &v: adj[*it]) if (v != heavy[*it]) dfs2(v);
            res[*it] = 0;
        }
    };
    dfs2(0);
    FOR(i, 1, N) if (res[P[i]]) res[i] = 1;
    return res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...