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: centroid_decomposition
(src/tree/centroid_decomposition.hpp)

centroid_decomposition

pait<Graph<int>, int> centroid_decomposition(Graph<T> g)

$n$ 頂点の木 g を入力すると重心分解した木 tree と,その根 root を返します.

tree[i][j] は頂点 $i$ まで木を分解したときにできる $i$ に隣接した連結成分の重心を指しています.

制約

計算量

Depends on

Verified with

Code

#pragma once
#include "../template/template.hpp"
#include "../graph/graph_template.hpp"
template <typename T>
pair<Graph<int>, int> centroid_decomposition(const Graph<T>& g) {
    const int n = g.size();
    vector<int> sub(n);
    vector<bool> visited(n);
    Graph<int> tree(n);
    auto get_size = [&](const auto& get_size, const int cur, const int par) -> int {
        sub[cur] = 1;
        for(const Edge<T>& e : g[cur]) {
            if(e.to == par or visited[e.to]) continue;
            sub[cur] += get_size(get_size, e.to, cur);
        }
        return sub[cur];
    };
    auto get_centroid = [&](const auto& get_centroid, const int cur, const int par, const int mid) -> int {
        for(const Edge<T>& e : g[cur]) {
            if(e.to == par or visited[e.to]) continue;
            if(sub[e.to] > mid) return get_centroid(get_centroid, e.to, cur, mid);
        }
        return cur;
    };
    auto dfs = [&](const auto& dfs, const int cur) -> int {
        const int centroid = get_centroid(get_centroid, cur, -1, get_size(get_size, cur, -1) / 2);
        visited[centroid] = true;
        for(const Edge<T>& e : g[centroid]) {
            if(visited[e.to]) continue;
            const int nex = dfs(dfs, e.to);
            if(centroid != nex) tree.add_directed_edge(centroid, nex);
        }
        visited[centroid] = false;
        return centroid;
    };
    const int root = dfs(dfs, 0);
    return {tree, root};
}
#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/centroid_decomposition.hpp"
template <typename T>
pair<Graph<int>, int> centroid_decomposition(const Graph<T>& g) {
    const int n = g.size();
    vector<int> sub(n);
    vector<bool> visited(n);
    Graph<int> tree(n);
    auto get_size = [&](const auto& get_size, const int cur, const int par) -> int {
        sub[cur] = 1;
        for(const Edge<T>& e : g[cur]) {
            if(e.to == par or visited[e.to]) continue;
            sub[cur] += get_size(get_size, e.to, cur);
        }
        return sub[cur];
    };
    auto get_centroid = [&](const auto& get_centroid, const int cur, const int par, const int mid) -> int {
        for(const Edge<T>& e : g[cur]) {
            if(e.to == par or visited[e.to]) continue;
            if(sub[e.to] > mid) return get_centroid(get_centroid, e.to, cur, mid);
        }
        return cur;
    };
    auto dfs = [&](const auto& dfs, const int cur) -> int {
        const int centroid = get_centroid(get_centroid, cur, -1, get_size(get_size, cur, -1) / 2);
        visited[centroid] = true;
        for(const Edge<T>& e : g[centroid]) {
            if(visited[e.to]) continue;
            const int nex = dfs(dfs, e.to);
            if(centroid != nex) tree.add_directed_edge(centroid, nex);
        }
        visited[centroid] = false;
        return centroid;
    };
    const int root = dfs(dfs, 0);
    return {tree, root};
}
Back to top page