Block-Cut Tree
(graph/block_cut_tree.hpp)
- View this file on GitHub
- Last update: 2026-08-13 01:41:40+09:00
- Include:
#include "graph/block_cut_tree.hpp"
Overview
This header converts a vertex-biconnected-components decomposition into its block-cut forest. Each block becomes one node, each articulation vertex becomes one node, and an edge joins a block to every articulation vertex it contains. For a connected input graph the result is a tree; for a disconnected graph it is a forest.
Every original vertex has a canonical node. An articulation vertex maps to its articulation node, while any other vertex maps to its unique block node. This is useful for reducing vertex-separator path queries to ordinary tree queries.
Graph Requirements
The same requirements as biconnected_components apply: build an undirected
graph with Graph<T>::add_edge; self-loops are unsupported, parallel edges are
supported, and inactive edges are ignored. Isolated vertices become isolated
block nodes.
Node Numbering
Block nodes have IDs in [0, block_count()), in the same order as
BiconnectedComponentsResult::components. Articulation nodes follow block
nodes, in increasing order of the original vertex ID.
API
struct BlockCutTreeResult {
std::vector<std::vector<int>> forest;
std::vector<int> node_of_block;
std::vector<int> node_of_articulation;
std::vector<int> node_of_vertex;
std::vector<int> block_of_node;
std::vector<int> articulation_of_node;
int node_count() const;
int block_count() const;
bool is_block_node(int node) const;
bool is_articulation_node(int node) const;
};
BlockCutTreeResult block_cut_tree(
const BiconnectedComponentsResult& biconnected
);
template <class T>
BlockCutTreeResult block_cut_tree(const Graph<T>& graph);
| Member or function | Description | Complexity |
|---|---|---|
forest[node] |
Adjacent nodes in the block-cut forest. | – |
node_of_block[block] |
Forest node representing block. |
$O(1)$ |
node_of_articulation[v] |
Articulation node for original vertex v, or -1 if v is not an articulation. |
$O(1)$ |
node_of_vertex[v] |
Canonical forest node representing original vertex v. |
$O(1)$ |
block_of_node[node] |
Original block ID, or -1 for an articulation node. |
$O(1)$ |
articulation_of_node[node] |
Original articulation vertex, or -1 for a block node. |
$O(1)$ |
node_count() |
Number of block and articulation nodes. | $O(1)$ |
block_count() |
Number of block nodes. | $O(1)$ |
is_block_node(node) |
Whether node represents a block. |
$O(1)$ |
is_articulation_node(node) |
Whether node represents an articulation vertex. |
$O(1)$ |
block_cut_tree(biconnected) |
Builds the forest from an existing decomposition. | $O(N + M)$ |
block_cut_tree(graph) |
Computes the decomposition and builds the forest. | $O(N + M)$ |
The graph overload does not mutate the input. The result owns its vectors and uses $O(N + M)$ memory. Use the decomposition overload when the blocks are also needed, so they are not computed twice.
Example
#include "graph/block_cut_tree.hpp"
#include <iostream>
int main() {
m1une::graph::Graph<> graph(5);
graph.add_edge(0, 1);
graph.add_edge(1, 2);
graph.add_edge(2, 0);
graph.add_edge(1, 3);
graph.add_edge(3, 4);
auto biconnected = m1une::graph::biconnected_components(graph);
auto block_cut = m1une::graph::block_cut_tree(biconnected);
std::cout << block_cut.node_count() << "\n"; // 5
std::cout << block_cut.node_of_articulation[1] << "\n";
std::cout << block_cut.node_of_vertex[0] << "\n";
}
Depends on
Required by
Verified with
verify/graph/block_cut_tree.test.cpp
verify/graph/cow_game.test.cpp
verify/graph/graph_algorithms.test.cpp
verify/graph/range_edge_graph.test.cpp
Code
#ifndef M1UNE_GRAPH_BLOCK_CUT_TREE_HPP
#define M1UNE_GRAPH_BLOCK_CUT_TREE_HPP 1
#include <cassert>
#include <vector>
#include "biconnected_components.hpp"
namespace m1une {
namespace graph {
struct BlockCutTreeResult {
std::vector<std::vector<int>> forest;
std::vector<int> node_of_block;
std::vector<int> node_of_articulation;
std::vector<int> node_of_vertex;
std::vector<int> block_of_node;
std::vector<int> articulation_of_node;
int node_count() const {
return int(forest.size());
}
int block_count() const {
return int(node_of_block.size());
}
bool is_block_node(int node) const {
assert(0 <= node && node < node_count());
return block_of_node[node] != -1;
}
bool is_articulation_node(int node) const {
assert(0 <= node && node < node_count());
return articulation_of_node[node] != -1;
}
};
// Builds the block-cut forest of a biconnected-components decomposition.
// Block nodes have IDs [0, block_count); articulation nodes follow them.
inline BlockCutTreeResult block_cut_tree(
const BiconnectedComponentsResult& biconnected
) {
const int vertex_count = int(biconnected.vertex_components.size());
const int block_count = biconnected.component_count();
BlockCutTreeResult result;
result.node_of_block.resize(block_count);
result.node_of_articulation.assign(vertex_count, -1);
result.node_of_vertex.assign(vertex_count, -1);
result.forest.resize(block_count);
result.block_of_node.resize(block_count);
result.articulation_of_node.assign(block_count, -1);
for (int block = 0; block < block_count; block++) {
result.node_of_block[block] = block;
result.block_of_node[block] = block;
}
for (int vertex = 0; vertex < vertex_count; vertex++) {
const std::vector<int>& blocks = biconnected.vertex_components[vertex];
assert(!blocks.empty());
if (blocks.size() == 1) {
assert(0 <= blocks[0] && blocks[0] < block_count);
result.node_of_vertex[vertex] = result.node_of_block[blocks[0]];
continue;
}
const int node = result.node_count();
result.node_of_articulation[vertex] = node;
result.node_of_vertex[vertex] = node;
result.forest.emplace_back();
result.block_of_node.push_back(-1);
result.articulation_of_node.push_back(vertex);
for (int block : blocks) {
assert(0 <= block && block < block_count);
const int block_node = result.node_of_block[block];
result.forest[node].push_back(block_node);
result.forest[block_node].push_back(node);
}
}
return result;
}
template <class T>
BlockCutTreeResult block_cut_tree(const Graph<T>& graph) {
return block_cut_tree(biconnected_components(graph));
}
} // namespace graph
} // namespace m1une
#endif // M1UNE_GRAPH_BLOCK_CUT_TREE_HPP#line 1 "graph/block_cut_tree.hpp"
#include <cassert>
#include <vector>
#line 1 "graph/biconnected_components.hpp"
#line 6 "graph/biconnected_components.hpp"
#line 1 "graph/graph.hpp"
#include <array>
#line 6 "graph/graph.hpp"
#include <utility>
#line 8 "graph/graph.hpp"
namespace m1une {
namespace graph {
template <class T = int>
struct Edge {
using cost_type = T;
int from;
int to;
T cost;
int id;
bool alive;
Edge() : from(-1), to(-1), cost(T()), id(-1), alive(true) {}
Edge(int from_, int to_, T cost_ = T(1), int id_ = -1, bool alive_ = true)
: from(from_), to(to_), cost(cost_), id(id_), alive(alive_) {}
int other(int v) const {
assert(v == from || v == to);
return from ^ to ^ v;
}
};
template <class T = int>
struct Graph {
using edge_type = Edge<T>;
using cost_type = T;
private:
struct EdgePositions {
std::array<std::pair<int, int>, 2> value{};
int size = 0;
void push_back(std::pair<int, int> position) {
assert(size < 2);
value[size++] = position;
}
};
int _n;
int _edge_count;
std::vector<std::vector<edge_type>> _g;
std::vector<EdgePositions> _edge_positions;
public:
Graph() : _n(0), _edge_count(0) {}
explicit Graph(int n) : _n(n), _edge_count(0), _g(n) {
assert(0 <= n);
}
int size() const {
return _n;
}
bool empty() const {
return _n == 0;
}
int edge_count() const {
return _edge_count;
}
int add_vertex() {
_g.emplace_back();
return _n++;
}
int add_directed_edge(int from, int to, T cost = T(1)) {
assert(0 <= from && from < _n);
assert(0 <= to && to < _n);
int id = _edge_count++;
int idx = int(_g[from].size());
_g[from].push_back(edge_type(from, to, cost, id));
_edge_positions.emplace_back();
_edge_positions.back().push_back({from, idx});
return id;
}
int add_edge(int u, int v, T cost = T(1)) {
assert(0 <= u && u < _n);
assert(0 <= v && v < _n);
int id = _edge_count++;
int u_idx = int(_g[u].size());
_g[u].push_back(edge_type(u, v, cost, id));
int v_idx = int(_g[v].size());
_g[v].push_back(edge_type(v, u, cost, id));
_edge_positions.emplace_back();
_edge_positions.back().push_back({u, u_idx});
_edge_positions.back().push_back({v, v_idx});
return id;
}
void set_edge_alive(int id, bool alive) {
assert(0 <= id && id < _edge_count);
for (int i = 0; i < _edge_positions[id].size; ++i) {
auto [v, idx] = _edge_positions[id].value[i];
_g[v][idx].alive = alive;
}
}
void erase_edge(int id) {
set_edge_alive(id, false);
}
void revive_edge(int id) {
set_edge_alive(id, true);
}
bool is_edge_alive(int id) const {
assert(0 <= id && id < _edge_count);
assert(_edge_positions[id].size != 0);
auto [v, idx] = _edge_positions[id].value[0];
return _g[v][idx].alive;
}
const std::vector<edge_type>& operator[](int v) const {
assert(0 <= v && v < _n);
return _g[v];
}
std::vector<edge_type>& operator[](int v) {
assert(0 <= v && v < _n);
return _g[v];
}
const std::vector<std::vector<edge_type>>& adjacency() const {
return _g;
}
std::vector<std::vector<edge_type>>& adjacency() {
return _g;
}
std::vector<edge_type> edges(bool include_inactive = false) const {
std::vector<edge_type> result;
result.reserve(_edge_count);
std::vector<char> used(_edge_count, false);
for (int v = 0; v < _n; v++) {
for (const auto& e : _g[v]) {
if (!include_inactive && !e.alive) continue;
if (0 <= e.id && e.id < _edge_count) {
if (used[e.id]) continue;
used[e.id] = true;
}
result.push_back(e);
}
}
return result;
}
Graph reversed() const {
Graph result(_n);
result._edge_count = _edge_count;
result._edge_positions.assign(_edge_count, {});
for (int v = 0; v < _n; v++) {
for (const auto& e : _g[v]) {
int idx = int(result._g[e.to].size());
result._g[e.to].push_back(edge_type(e.to, e.from, e.cost, e.id, e.alive));
if (0 <= e.id && e.id < _edge_count) result._edge_positions[e.id].push_back({e.to, idx});
}
}
return result;
}
};
} // namespace graph
} // namespace m1une
#line 8 "graph/biconnected_components.hpp"
namespace m1une {
namespace graph {
struct BiconnectedComponentsResult {
std::vector<std::vector<int>> components;
std::vector<std::vector<int>> edge_components;
std::vector<int> component_of_edge;
std::vector<std::vector<int>> vertex_components;
std::vector<int> articulation;
std::vector<int> ord;
std::vector<int> low;
int component_count() const {
return int(components.size());
}
bool is_articulation(int vertex) const {
assert(0 <= vertex && vertex < int(vertex_components.size()));
return vertex_components[vertex].size() >= 2;
}
};
// Decomposes an undirected graph into maximal vertex-biconnected blocks.
// Every active edge belongs to exactly one block. Isolated vertices form
// singleton blocks, and articulation vertices occur in multiple blocks.
template <class T>
BiconnectedComponentsResult biconnected_components(const Graph<T>& graph) {
const int n = graph.size();
const int edge_count = graph.edge_count();
BiconnectedComponentsResult result;
result.component_of_edge.assign(edge_count, -1);
result.vertex_components.assign(n, {});
result.ord.assign(n, -1);
result.low.assign(n, -1);
std::vector<int> edge_from(edge_count, -1);
std::vector<int> edge_to(edge_count, -1);
std::vector<int> incidence_count(edge_count, 0);
std::vector<int> alive_degree(n, 0);
for (int vertex = 0; vertex < n; vertex++) {
for (const Edge<T>& edge : graph[vertex]) {
if (!edge.alive) continue;
assert(0 <= edge.id && edge.id < edge_count);
alive_degree[vertex]++;
if (incidence_count[edge.id] == 0) {
edge_from[edge.id] = edge.from;
edge_to[edge.id] = edge.to;
}
incidence_count[edge.id]++;
}
}
#ifndef NDEBUG
for (int edge_id = 0; edge_id < edge_count; edge_id++) {
if (incidence_count[edge_id] == 0) continue;
assert(incidence_count[edge_id] == 2);
assert(edge_from[edge_id] != edge_to[edge_id]);
}
#endif
std::vector<int> parent(n, -1);
std::vector<int> parent_edge(n, -1);
std::vector<int> next_edge(n, 0);
std::vector<int> dfs_stack;
std::vector<int> edge_stack;
std::vector<int> vertex_mark(n, -1);
int timer = 0;
auto add_singleton = [&](int vertex) {
const int component = result.component_count();
result.components.push_back(std::vector<int>(1, vertex));
result.edge_components.emplace_back();
result.vertex_components[vertex].push_back(component);
};
auto extract_component = [&](int stopping_edge) {
const int component = result.component_count();
result.components.emplace_back();
result.edge_components.emplace_back();
std::vector<int>& vertices = result.components.back();
std::vector<int>& edges = result.edge_components.back();
while (true) {
assert(!edge_stack.empty());
const int edge_id = edge_stack.back();
edge_stack.pop_back();
edges.push_back(edge_id);
result.component_of_edge[edge_id] = component;
const int endpoints[2] = {edge_from[edge_id], edge_to[edge_id]};
for (int vertex : endpoints) {
if (vertex_mark[vertex] == component) continue;
vertex_mark[vertex] = component;
vertices.push_back(vertex);
}
if (edge_id == stopping_edge) break;
}
for (int vertex : vertices) {
result.vertex_components[vertex].push_back(component);
}
};
for (int root = 0; root < n; root++) {
if (result.ord[root] != -1) continue;
if (alive_degree[root] == 0) {
result.ord[root] = result.low[root] = timer++;
add_singleton(root);
continue;
}
result.ord[root] = result.low[root] = timer++;
dfs_stack.push_back(root);
while (!dfs_stack.empty()) {
const int vertex = dfs_stack.back();
if (next_edge[vertex] < int(graph[vertex].size())) {
const Edge<T>& edge = graph[vertex][next_edge[vertex]++];
if (!edge.alive || edge.id == parent_edge[vertex]) continue;
const int to = edge.to;
if (result.ord[to] == -1) {
parent[to] = vertex;
parent_edge[to] = edge.id;
edge_stack.push_back(edge.id);
result.ord[to] = result.low[to] = timer++;
dfs_stack.push_back(to);
} else if (result.ord[to] < result.ord[vertex]) {
edge_stack.push_back(edge.id);
if (result.ord[to] < result.low[vertex]) {
result.low[vertex] = result.ord[to];
}
}
continue;
}
dfs_stack.pop_back();
const int parent_vertex = parent[vertex];
if (parent_vertex == -1) {
assert(edge_stack.empty());
continue;
}
if (result.low[vertex] < result.low[parent_vertex]) {
result.low[parent_vertex] = result.low[vertex];
}
if (result.ord[parent_vertex] <= result.low[vertex]) {
extract_component(parent_edge[vertex]);
}
}
}
for (int vertex = 0; vertex < n; vertex++) {
if (result.is_articulation(vertex)) result.articulation.push_back(vertex);
}
return result;
}
} // namespace graph
} // namespace m1une
#line 8 "graph/block_cut_tree.hpp"
namespace m1une {
namespace graph {
struct BlockCutTreeResult {
std::vector<std::vector<int>> forest;
std::vector<int> node_of_block;
std::vector<int> node_of_articulation;
std::vector<int> node_of_vertex;
std::vector<int> block_of_node;
std::vector<int> articulation_of_node;
int node_count() const {
return int(forest.size());
}
int block_count() const {
return int(node_of_block.size());
}
bool is_block_node(int node) const {
assert(0 <= node && node < node_count());
return block_of_node[node] != -1;
}
bool is_articulation_node(int node) const {
assert(0 <= node && node < node_count());
return articulation_of_node[node] != -1;
}
};
// Builds the block-cut forest of a biconnected-components decomposition.
// Block nodes have IDs [0, block_count); articulation nodes follow them.
inline BlockCutTreeResult block_cut_tree(
const BiconnectedComponentsResult& biconnected
) {
const int vertex_count = int(biconnected.vertex_components.size());
const int block_count = biconnected.component_count();
BlockCutTreeResult result;
result.node_of_block.resize(block_count);
result.node_of_articulation.assign(vertex_count, -1);
result.node_of_vertex.assign(vertex_count, -1);
result.forest.resize(block_count);
result.block_of_node.resize(block_count);
result.articulation_of_node.assign(block_count, -1);
for (int block = 0; block < block_count; block++) {
result.node_of_block[block] = block;
result.block_of_node[block] = block;
}
for (int vertex = 0; vertex < vertex_count; vertex++) {
const std::vector<int>& blocks = biconnected.vertex_components[vertex];
assert(!blocks.empty());
if (blocks.size() == 1) {
assert(0 <= blocks[0] && blocks[0] < block_count);
result.node_of_vertex[vertex] = result.node_of_block[blocks[0]];
continue;
}
const int node = result.node_count();
result.node_of_articulation[vertex] = node;
result.node_of_vertex[vertex] = node;
result.forest.emplace_back();
result.block_of_node.push_back(-1);
result.articulation_of_node.push_back(vertex);
for (int block : blocks) {
assert(0 <= block && block < block_count);
const int block_node = result.node_of_block[block];
result.forest[node].push_back(block_node);
result.forest[block_node].push_back(node);
}
}
return result;
}
template <class T>
BlockCutTreeResult block_cut_tree(const Graph<T>& graph) {
return block_cut_tree(biconnected_components(graph));
}
} // namespace graph
} // namespace m1une