kyopro-lib-cpp

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub packer-jp/kyopro-lib-cpp

:heavy_check_mark: test/unit/hld.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A&lang=ja"
#include "../../data_structure/segtree.hpp"
#include "../../graph/hld.hpp"

#include <bits/stdc++.h>
using namespace std;

int main() {
    ll n = 10;
    struct str_monoid {
        using V = string;
        static V op(V a, V b) { return a + b; }
        static V e() { return ""; }
    };
    segtree<str_monoid> st_fwd(n), st_rev(n);
    vector<string> weight = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
    hld hld(n);
    hld.add_edge(7, 2);
    hld.add_edge(2, 6);
    hld.add_edge(4, 2);
    hld.add_edge(4, 3);
    hld.add_edge(5, 4);
    hld.add_edge(5, 1);
    hld.add_edge(1, 0);
    hld.add_edge(0, 9);
    hld.add_edge(5, 8);
    vector<ll> in = hld.build(5);
    for (ll i : rep(n)) {
        st_fwd.set(in[i], weight[i]);
        st_rev.set(n - in[i] - 1, weight[i]);
    }
    auto path = [&](ll u, ll v) {
        string ret = "";
        vector<pair<ll, ll>> path = hld.get_path(u, v, false);
        for (ll i : rep(path.size())) {
            ll l = path[i].first, r = path[i].second;
            if (l < r) {
                ret += st_fwd.prod(l, r + 1);
            } else {
                ret += st_rev.prod(n - l - 1, n - r);
            }
        }
        return ret;
    };
    auto subtree = [&](ll v) {
        pair<ll, ll> subtree = hld.get_subtree(v, false);
        return st_fwd.prod(subtree.first, subtree.second + 1);
    };
    assert(path(2, 1) == "cefb");
    assert(path(5, 3) == "fed");
    assert(path(7, 4) == "hce");
    assert(path(4, 4) == "e");
    assert(subtree(2) == "chg");
    assert(subtree(5) == "fechgdbaji");
    assert(hld.dist(2, 0) == 4);
    assert(hld.dist(4, 1) == 2);

    cout << "Hello World" << endl;
}
#line 1 "test/unit/hld.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A&lang=ja"
#line 2 "data_structure/segtree.hpp"

#line 2 "template.hpp"

#include <bits/stdc++.h>
using namespace std;

#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define uniq(a) (a).erase(unique(all(a)), (a).end())
#define t0 first
#define t1 second
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
constexpr double pi = 3.14159265358979323846;
constexpr ll dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr ll dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr ll sign(ll a) { return (a > 0) - (a < 0); }
constexpr ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
constexpr ll cdiv(ll a, ll b) { return -fdiv(-a, b); }
constexpr ll pw(ll n) { return 1ll << n; }
constexpr ll flg(ll n) { return 63 - __builtin_clzll(n); }
constexpr ll clg(ll n) { return n == 1 ? 0 : flg(n - 1) + 1; }
constexpr ll safemod(ll x, ll mod) { return (x % mod + mod) % mod; }
template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template <typename T> constexpr T sq(const T &a) { return a * a; }
template <typename T, typename U> constexpr bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <typename T, typename U> constexpr bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &a) {
    os << "(" << a.first << ", " << a.second << ")";
    return os;
}
template <typename T, typename U, typename V> ostream &operator<<(ostream &os, const tuple<T, U, V> &a) {
    os << "(" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
#ifdef ONLINE_JUDGE
#define dump(...) (void(0))
#else
void debug() { cerr << endl; }
template <typename Head, typename... Tail> void debug(Head &&head, Tail &&... tail) {
    cerr << head;
    if (sizeof...(Tail)) cerr << ", ";
    debug(tail...);
}
#define dump(...) cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ", debug(__VA_ARGS__)
#endif
struct rep {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { ++v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v < *i; }
    };
    ll l, r;
    rep(ll l, ll r) : l(l), r(r) {}
    rep(ll r) : rep(0, r) {}
    itr begin() const { return l; };
    itr end() const { return r; };
};
struct per {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { --v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v > *i; }
    };
    ll l, r;
    per(ll l, ll r) : l(l), r(r) {}
    per(ll r) : per(0, r) {}
    itr begin() const { return r - 1; };
    itr end() const { return l - 1; };
};
struct io_setup {
    static constexpr int PREC = 20;
    io_setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} iOS;
#line 4 "data_structure/segtree.hpp"

template <typename P> struct segtree {
    using V = typename P::V;
    ll n, size;
    vector<V> val;
    segtree(ll n) : segtree(vector(n, P::e())) {}
    segtree(const vector<V> &src) : n(src.size()) {
        size = pw(clg(n));
        val.resize(size << 1);
        copy(all(src), val.begin() + size);
        for (ll i : per(1, size)) val[i] = P::op(val[i << 1 | 0], val[i << 1 | 1]);
    }
    void set(ll i, const V &a) {
        val[i += size] = a;
        while (i >>= 1) val[i] = P::op(val[i << 1 | 0], val[i << 1 | 1]);
    }
    V get(ll i) const { return val[i + size]; }
    V prod(ll l, ll r) const {
        V a = P::e(), b = P::e();
        for (l += size, r += size; l < r; l >>= 1, r >>= 1) {
            if (l & 1) a = P::op(a, val[l++]);
            if (r & 1) b = P::op(val[--r], b);
        }
        return P::op(a, b);
    }
    template <typename F> ll max_right(ll l, F f) const {
        if (l == n) return n;
        V a = P::e();
        l += size;
        do {
            while (~l & 1) l >>= 1;
            if (!f(P::op(a, val[l]))) {
                while (l < size) {
                    l = l << 1;
                    if (f(P::op(a, val[l]))) a = P::op(a, val[l++]);
                }
                return l - size;
            }
            a = P::op(a, val[l++]);
        } while ((l & -l) != l);
        return n;
    }
    template <typename F> ll min_left(ll r, F f) const {
        if (r == 0) return 0;
        V a = P::e();
        r += size;
        do {
            r--;
            while (r > 1 && r & 1) r >>= 1;
            if (!f(P::op(val[r], a))) {
                while (r < size) {
                    r = r << 1 | 1;
                    if (f(P::op(val[r], a))) a = P::op(val[r--], a);
                }
                return r + 1 - size;
            }
            a = P::op(val[r], a);
        } while ((r & -r) != r);
        return 0;
    }
};

struct min_monoid {
    using V = ll;
    static V op(V a, V b) { return min(a, b); }
    static V e() { return LLONG_MAX; }
};

struct sum_monoid {
    using V = ll;
    static V op(V a, V b) { return a + b; }
    static V e() { return 0; }
};
#line 2 "graph/hld.hpp"

#line 4 "graph/hld.hpp"

struct hld {
    vector<vector<ll>> g;
    vector<ll> par, sz, dep, in, out, head;
    hld(ll n) : g(n), par(n), sz(n), dep(n), in(n), out(n), head(n) {}
    void add_edge(ll u, ll v) { g[u].push_back(v), g[v].push_back(u); }
    vector<ll> build(ll root) {
        auto dfs_sz = [&](auto dfs_sz, ll v, ll p) -> void {
            sz[v] = 1;
            par[v] = p;
            if (p != -1) dep[v] = dep[p] + 1;
            for (ll &u : g[v]) {
                if (u == p) continue;
                dfs_sz(dfs_sz, u, v);
                sz[v] += sz[u];
                if (sz[u] > sz[g[v][0]]) swap(u, g[v][0]);
            }
        };
        ll t = 0;
        auto dfs_hld = [&](auto dfs_hld, ll v) -> ll {
            in[v] = t++;
            for (ll i : rep(g[v].size())) {
                ll u = g[v][i];
                if (u == par[v]) continue;
                head[u] = (i == 0 ? head[v] : u);
                dfs_hld(dfs_hld, u);
            }
            return out[v] = t;
        };
        dfs_sz(dfs_sz, root, -1);
        head[root] = root;
        dfs_hld(dfs_hld, root);
        return in;
    }
    ll lca(ll u, ll v) const {
        while (true) {
            if (in[u] > in[v]) swap(u, v);
            if (head[u] == head[v]) return u;
            v = par[head[v]];
        }
    }
    ll dist(ll u, ll v) const { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }
    vector<pair<ll, ll>> get_path(ll u, ll v, bool edge) const {
        vector<pair<ll, ll>> a, b;
        while (true) {
            if (head[u] == head[v]) {
                if (edge) {
                    if (in[u] > in[v]) a.emplace_back(in[u], in[v] + 1);
                    if (in[u] < in[v]) a.emplace_back(in[u] + 1, in[v]);
                } else {
                    a.emplace_back(in[u], in[v]);
                }
                break;
            }
            if (in[u] > in[v]) {
                a.emplace_back(in[u], in[head[u]]);
                u = par[head[u]];
            } else {
                b.emplace_back(in[head[v]], in[v]);
                v = par[head[v]];
            }
        }
        a.insert(a.end(), b.rbegin(), b.rend());
        return a;
    }
    pair<ll, ll> get_subtree(ll v, bool edge) const { return {in[v] + edge, out[v] - 1}; }
};
#line 4 "test/unit/hld.test.cpp"

#line 6 "test/unit/hld.test.cpp"
using namespace std;

int main() {
    ll n = 10;
    struct str_monoid {
        using V = string;
        static V op(V a, V b) { return a + b; }
        static V e() { return ""; }
    };
    segtree<str_monoid> st_fwd(n), st_rev(n);
    vector<string> weight = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
    hld hld(n);
    hld.add_edge(7, 2);
    hld.add_edge(2, 6);
    hld.add_edge(4, 2);
    hld.add_edge(4, 3);
    hld.add_edge(5, 4);
    hld.add_edge(5, 1);
    hld.add_edge(1, 0);
    hld.add_edge(0, 9);
    hld.add_edge(5, 8);
    vector<ll> in = hld.build(5);
    for (ll i : rep(n)) {
        st_fwd.set(in[i], weight[i]);
        st_rev.set(n - in[i] - 1, weight[i]);
    }
    auto path = [&](ll u, ll v) {
        string ret = "";
        vector<pair<ll, ll>> path = hld.get_path(u, v, false);
        for (ll i : rep(path.size())) {
            ll l = path[i].first, r = path[i].second;
            if (l < r) {
                ret += st_fwd.prod(l, r + 1);
            } else {
                ret += st_rev.prod(n - l - 1, n - r);
            }
        }
        return ret;
    };
    auto subtree = [&](ll v) {
        pair<ll, ll> subtree = hld.get_subtree(v, false);
        return st_fwd.prod(subtree.first, subtree.second + 1);
    };
    assert(path(2, 1) == "cefb");
    assert(path(5, 3) == "fed");
    assert(path(7, 4) == "hce");
    assert(path(4, 4) == "e");
    assert(subtree(2) == "chg");
    assert(subtree(5) == "fechgdbaji");
    assert(hld.dist(2, 0) == 4);
    assert(hld.dist(4, 1) == 2);

    cout << "Hello World" << endl;
}
Back to top page