Fu_L's Library

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

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: AuxiliaryTree
(src/tree/auxiliary_tree.hpp)

AuxiliaryTree

$n$ 頂点の根付き木が与えられたとき,

を前計算 $O(n)$ クエリ $O( U \log n)$ で求めます.

コンストラクタ

AuxiliaryTree<T> aux(Graph<T> g, int root = 0)

制約

計算量

get

pair<Graph<int>, vector<int>> aux.get(vector<int> idx)

頂点集合 idx を指定したときのAuxiliary Treeと,Auxiliary Treeと元の木の頂点番号の対応配列を返します.

計算量

$m$ を頂点集合 idx のサイズとして,

Depends on

Verified with

Code

#pragma once
#include "../template/template.hpp"
#include "../graph/graph_template.hpp"
#include "./heavy_light_decomposition.hpp"
template <typename T>
struct AuxiliaryTree {
    AuxiliaryTree(const Graph<T>& _g, const int root = 0)
        : g(_g), hld(g, root) {}
    pair<Graph<int>, vector<int>> get(vector<int> idx) {
        if(idx.empty()) return {Graph<int>(0), {}};
        auto comp = [&](const int i, const int j) {
            return hld.idx(i).first < hld.idx(j).first;
        };
        sort(begin(idx), end(idx), comp);
        for(int i = 0, ie = idx.size(); i + 1 < ie; ++i) {
            idx.push_back(hld.lca(idx[i], idx[i + 1]));
        }
        sort(begin(idx), end(idx), comp);
        idx.erase(unique(begin(idx), end(idx)), end(idx));
        Graph<int> aux(idx.size());
        vector<int> rs;
        rs.push_back(0);
        for(int i = 1; i < (int)idx.size(); ++i) {
            const int l = hld.lca(idx[rs.back()], idx[i]);
            while(idx[rs.back()] != l) rs.pop_back();
            aux.add_directed_edge(rs.back(), i);
            rs.push_back(i);
        }
        return make_pair(aux, idx);
    }

   private:
    Graph<T> g;
    HeavyLightDecomposition<T> hld;
};
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T>
struct Graph {
    Graph(const int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 4 "src/tree/heavy_light_decomposition.hpp"
template <typename T>
struct HeavyLightDecomposition {
    HeavyLightDecomposition(Graph<T>& _g, const int root = 0)
        : g(_g), n(g.size()), id(0), sz(n, 0), dep(n, 0), down(n, -1), up(n, -1), nex(n, root), par(n, -1), rev(n, 0), co(n, 0) {
        assert(0 <= root and root < n);
        dfs_sz(root);
        dfs_hld(root);
    }
    pair<int, int> idx(const int i) const {
        assert(0 <= i and i < n);
        return make_pair(down[i], up[i]);
    }
    int depth(const int v) const {
        assert(0 <= v and v < n);
        return dep[v];
    }
    T cost(const int v) const {
        assert(0 <= v and v < n);
        return co[v];
    }
    int parent(const int v) const {
        assert(0 <= v and v < n);
        return par[v];
    }
    int la(int v, int x) const {
        assert(0 <= v and v < n);
        assert(x >= 0);
        if(x > dep[v]) return -1;
        while(true) {
            const int u = nex[v];
            if(down[v] - x >= down[u]) return rev[down[v] - x];
            x -= down[v] - down[u] + 1;
            v = par[u];
        }
    }
    int lca(int u, int v) const {
        assert(0 <= u and u < n);
        assert(0 <= v and v < n);
        while(nex[u] != nex[v]) {
            if(down[u] < down[v]) swap(u, v);
            u = par[nex[u]];
        }
        return dep[u] < dep[v] ? u : v;
    }
    int dist(const int u, const int v) const {
        assert(0 <= u and u < n);
        assert(0 <= v and v < n);
        return dep[u] + dep[v] - dep[lca(u, v)] * 2;
    }
    T length(const int u, const int v) const {
        assert(0 <= u and u < n);
        assert(0 <= v and v < n);
        return co[u] + co[v] - co[lca(u, v)] * 2;
    }
    template <typename F>
    void path_query(const int u, const int v, const bool vertex, const F& f) {
        assert(0 <= u and u < n);
        assert(0 <= v and v < n);
        const int l = lca(u, v);
        for(auto&& [a, b] : ascend(u, l)) f(a + 1, b);
        if(vertex) f(down[l], down[l] + 1);
        for(auto&& [a, b] : descend(l, v)) f(a, b + 1);
    }
    template <typename F>
    void subtree_query(const int v, const bool vertex, const F& f) {
        assert(0 <= v and v < n);
        f(down[v] + int(!vertex), up[v]);
    }

   private:
    Graph<T>& g;
    int n, id;
    vector<int> sz, dep, down, up, nex, par, rev;
    vector<T> co;
    void dfs_sz(const int cur) {
        sz[cur] = 1;
        for(Edge<T>& edge : g[cur]) {
            if(edge.to == par[cur]) {
                if(g[cur].size() >= 2 and edge.to == g[cur][0].to) {
                    swap(g[cur][0], g[cur][1]);
                } else {
                    continue;
                }
            }
            dep[edge.to] = dep[cur] + 1;
            co[edge.to] = co[cur] + edge.cost;
            par[edge.to] = cur;
            dfs_sz(edge.to);
            sz[cur] += sz[edge.to];
            if(sz[edge.to] > sz[g[cur][0].to]) {
                swap(edge, g[cur][0]);
            }
        }
    }
    void dfs_hld(const int cur) {
        down[cur] = id++;
        rev[down[cur]] = cur;
        for(const Edge<T>& edge : g[cur]) {
            if(edge.to == par[cur]) continue;
            nex[edge.to] = (edge.to == g[cur][0].to ? nex[cur] : edge.to);
            dfs_hld(edge.to);
        }
        up[cur] = id;
    }
    vector<pair<int, int>> ascend(int u, int v) const {
        vector<pair<int, int>> res;
        while(nex[u] != nex[v]) {
            res.emplace_back(down[u], down[nex[u]]);
            u = par[nex[u]];
        }
        if(u != v) res.emplace_back(down[u], down[v] + 1);
        return res;
    }
    vector<pair<int, int>> descend(const int u, const int v) const {
        if(u == v) return {};
        if(nex[u] == nex[v]) return {{down[u] + 1, down[v]}};
        auto res = descend(u, par[nex[v]]);
        res.emplace_back(down[nex[v]], down[v]);
        return res;
    }
};
#line 5 "src/tree/auxiliary_tree.hpp"
template <typename T>
struct AuxiliaryTree {
    AuxiliaryTree(const Graph<T>& _g, const int root = 0)
        : g(_g), hld(g, root) {}
    pair<Graph<int>, vector<int>> get(vector<int> idx) {
        if(idx.empty()) return {Graph<int>(0), {}};
        auto comp = [&](const int i, const int j) {
            return hld.idx(i).first < hld.idx(j).first;
        };
        sort(begin(idx), end(idx), comp);
        for(int i = 0, ie = idx.size(); i + 1 < ie; ++i) {
            idx.push_back(hld.lca(idx[i], idx[i + 1]));
        }
        sort(begin(idx), end(idx), comp);
        idx.erase(unique(begin(idx), end(idx)), end(idx));
        Graph<int> aux(idx.size());
        vector<int> rs;
        rs.push_back(0);
        for(int i = 1; i < (int)idx.size(); ++i) {
            const int l = hld.lca(idx[rs.back()], idx[i]);
            while(idx[rs.back()] != l) rs.pop_back();
            aux.add_directed_edge(rs.back(), i);
            rs.push_back(i);
        }
        return make_pair(aux, idx);
    }

   private:
    Graph<T> g;
    HeavyLightDecomposition<T> hld;
};
Back to top page