Euler Tour
(graph/tree/euler_tour.hpp)
- View this file on GitHub
- Last update: 2026-08-13 01:41:40+09:00
- Include:
#include "graph/tree/euler_tour.hpp"
Overview
m1une::tree::EulerTour<T> roots an undirected tree and flattens each rooted
subtree into one contiguous interval. It is the lightweight choice when you only
need parent/depth metadata and subtree ranges for a Fenwick tree, segment tree,
or direct iteration.
The input uses m1une::graph::Graph<T> and should be built with add_edge.
Inactive graph edges are ignored.
Construction
m1une::graph::Graph<int> g(n);
g.add_edge(u, v);
m1une::tree::EulerTour<int> tour(g, 0);
The graph is expected to be an undirected tree. If the graph is disconnected,
only the component reachable from the selected root is represented; unreachable
vertices keep tin[v] == -1.
Public Members
For a connected tree, all arrays have size N, and order also contains N
vertices.
| Member | Type | Description |
|---|---|---|
root |
int |
Root vertex, or -1 for an empty tree. |
parent |
std::vector<int> |
Parent vertex, or -1 at the root. |
parent_edge |
std::vector<int> |
Edge id connecting the vertex to its parent, or -1. |
depth |
std::vector<int> |
Number of edges from the root. |
dist |
std::vector<T> |
Sum of edge costs from the root. |
subtree_size |
std::vector<int> |
Number of vertices in each rooted subtree. |
tin, tout
|
std::vector<int> |
Subtree interval [tin[v], tout[v]) in preorder. |
order |
std::vector<int> |
Vertices in DFS preorder. |
children |
std::vector<std::vector<int>> |
Children in the rooted tree. |
Important relationships:
- Vertex
vis stored at base-array indextin[v]. -
order[i]converts a base-array index back to the original vertex. - The rooted subtree of
vis exactlyorder[tin[v]..tout[v]). - If edge values are stored at the child vertex position, the subtree edge
interval is
[tin[v] + 1, tout[v]).
Methods
| Method | Description | Complexity |
|---|---|---|
EulerTour(const Graph<T>& g, int root = 0) |
Builds Euler-tour metadata. | $O(N)$ |
void build(const Graph<T>& g, int root = 0) |
Rebuilds the structure. | $O(N)$ |
int size() const |
Returns the number of vertices in the source graph. | $O(1)$ |
int visited_size() const |
Returns the number of vertices reached from root. |
$O(1)$ |
bool empty() const |
Returns whether the source graph is empty. | $O(1)$ |
bool is_ancestor(int u, int v) const |
Returns whether u is an ancestor of v. |
$O(1)$ |
bool in_subtree(int v, int u) const |
Returns whether v is in the subtree of u. |
$O(1)$ |
std::pair<int, int> subtree_range(int v, bool edge = false) const |
Returns [tin[v], tout[v]); with edge=true, excludes v. |
$O(1)$ |
std::vector<int> subtree_vertices(int v) const |
Returns vertices in the rooted subtree of v. |
$O(\text{subtree size})$ |
template <class F> void for_each_subtree(int v, F f) const |
Calls f(vertex) for each vertex in the subtree. |
$O(\text{subtree size})$ |
T must be default-constructible and support addition with edge costs if
dist is used.
Example
#include "ds/range_query/fenwick_tree.hpp"
#include "graph/graph.hpp"
#include "graph/tree/euler_tour.hpp"
#include <iostream>
#include <vector>
int main() {
m1une::graph::Graph<int> g(4);
g.add_edge(0, 1);
g.add_edge(0, 2);
g.add_edge(1, 3);
std::vector<long long> value = {10, 20, 30, 40};
m1une::tree::EulerTour<int> tour(g, 0);
std::vector<long long> base(4);
for (int v = 0; v < 4; ++v) {
base[tour.tin[v]] = value[v];
}
m1une::ds::FenwickTree<long long> fenwick(base);
auto [l, r] = tour.subtree_range(1);
std::cout << fenwick.sum(l, r) << "\n"; // 60
}
Depends on
Required by
Verified with
verify/graph/cow_game.test.cpp
verify/graph/graph_algorithms.test.cpp
verify/graph/range_edge_graph.test.cpp
verify/graph/tree/tree_algorithms.test.cpp
verify/graph/tree/vertex_add_subtree_sum.test.cpp
Code
#ifndef M1UNE_TREE_EULER_TOUR_HPP
#define M1UNE_TREE_EULER_TOUR_HPP 1
#include <algorithm>
#include <cassert>
#include <utility>
#include <vector>
#include "../graph.hpp"
namespace m1une {
namespace tree {
template <class T = int>
struct EulerTour {
using cost_type = T;
using edge_type = m1une::graph::Edge<T>;
int root;
std::vector<int> parent;
std::vector<int> parent_edge;
std::vector<int> depth;
std::vector<T> dist;
std::vector<int> subtree_size;
std::vector<int> tin;
std::vector<int> tout;
std::vector<int> order;
std::vector<std::vector<int>> children;
private:
int _n;
void check_vertex(int v) const {
assert(0 <= v && v < _n);
assert(tin[v] != -1);
}
public:
EulerTour() : root(-1), _n(0) {}
explicit EulerTour(const m1une::graph::Graph<T>& g, int root_ = 0) {
build(g, root_);
}
void build(const m1une::graph::Graph<T>& g, int root_ = 0) {
_n = g.size();
root = _n == 0 ? -1 : root_;
parent.assign(_n, -2);
parent_edge.assign(_n, -1);
depth.assign(_n, 0);
dist.assign(_n, T(0));
subtree_size.assign(_n, 0);
tin.assign(_n, -1);
tout.assign(_n, -1);
order.clear();
order.reserve(_n);
children.assign(_n, {});
if (_n == 0) return;
assert(0 <= root && root < _n);
struct Frame {
int v;
int state;
};
std::vector<Frame> stack;
stack.push_back({root, 0});
parent[root] = -1;
while (!stack.empty()) {
Frame frame = stack.back();
stack.pop_back();
int v = frame.v;
if (frame.state == 0) {
tin[v] = int(order.size());
order.push_back(v);
stack.push_back({v, 1});
const auto& adj = g[v];
for (int i = int(adj.size()) - 1; i >= 0; --i) {
const auto& e = adj[i];
if (!e.alive) continue;
if (parent[e.to] != -2) continue;
parent[e.to] = v;
parent_edge[e.to] = e.id;
depth[e.to] = depth[v] + 1;
dist[e.to] = dist[v] + e.cost;
children[v].push_back(e.to);
stack.push_back({e.to, 0});
}
std::reverse(children[v].begin(), children[v].end());
} else {
subtree_size[v] = 1;
for (int child : children[v]) subtree_size[v] += subtree_size[child];
tout[v] = int(order.size());
}
}
}
int size() const {
return _n;
}
int visited_size() const {
return int(order.size());
}
bool empty() const {
return _n == 0;
}
bool is_ancestor(int u, int v) const {
check_vertex(u);
check_vertex(v);
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
bool in_subtree(int v, int u) const {
return is_ancestor(u, v);
}
std::pair<int, int> subtree_range(int v, bool edge = false) const {
check_vertex(v);
return {tin[v] + (edge ? 1 : 0), tout[v]};
}
std::vector<int> subtree_vertices(int v) const {
check_vertex(v);
return std::vector<int>(order.begin() + tin[v], order.begin() + tout[v]);
}
template <class F>
void for_each_subtree(int v, F f) const {
auto [l, r] = subtree_range(v);
for (int i = l; i < r; ++i) f(order[i]);
}
};
} // namespace tree
} // namespace m1une
#endif // M1UNE_TREE_EULER_TOUR_HPP#line 1 "graph/tree/euler_tour.hpp"
#include <algorithm>
#include <cassert>
#include <utility>
#include <vector>
#line 1 "graph/graph.hpp"
#include <array>
#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 10 "graph/tree/euler_tour.hpp"
namespace m1une {
namespace tree {
template <class T = int>
struct EulerTour {
using cost_type = T;
using edge_type = m1une::graph::Edge<T>;
int root;
std::vector<int> parent;
std::vector<int> parent_edge;
std::vector<int> depth;
std::vector<T> dist;
std::vector<int> subtree_size;
std::vector<int> tin;
std::vector<int> tout;
std::vector<int> order;
std::vector<std::vector<int>> children;
private:
int _n;
void check_vertex(int v) const {
assert(0 <= v && v < _n);
assert(tin[v] != -1);
}
public:
EulerTour() : root(-1), _n(0) {}
explicit EulerTour(const m1une::graph::Graph<T>& g, int root_ = 0) {
build(g, root_);
}
void build(const m1une::graph::Graph<T>& g, int root_ = 0) {
_n = g.size();
root = _n == 0 ? -1 : root_;
parent.assign(_n, -2);
parent_edge.assign(_n, -1);
depth.assign(_n, 0);
dist.assign(_n, T(0));
subtree_size.assign(_n, 0);
tin.assign(_n, -1);
tout.assign(_n, -1);
order.clear();
order.reserve(_n);
children.assign(_n, {});
if (_n == 0) return;
assert(0 <= root && root < _n);
struct Frame {
int v;
int state;
};
std::vector<Frame> stack;
stack.push_back({root, 0});
parent[root] = -1;
while (!stack.empty()) {
Frame frame = stack.back();
stack.pop_back();
int v = frame.v;
if (frame.state == 0) {
tin[v] = int(order.size());
order.push_back(v);
stack.push_back({v, 1});
const auto& adj = g[v];
for (int i = int(adj.size()) - 1; i >= 0; --i) {
const auto& e = adj[i];
if (!e.alive) continue;
if (parent[e.to] != -2) continue;
parent[e.to] = v;
parent_edge[e.to] = e.id;
depth[e.to] = depth[v] + 1;
dist[e.to] = dist[v] + e.cost;
children[v].push_back(e.to);
stack.push_back({e.to, 0});
}
std::reverse(children[v].begin(), children[v].end());
} else {
subtree_size[v] = 1;
for (int child : children[v]) subtree_size[v] += subtree_size[child];
tout[v] = int(order.size());
}
}
}
int size() const {
return _n;
}
int visited_size() const {
return int(order.size());
}
bool empty() const {
return _n == 0;
}
bool is_ancestor(int u, int v) const {
check_vertex(u);
check_vertex(v);
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
bool in_subtree(int v, int u) const {
return is_ancestor(u, v);
}
std::pair<int, int> subtree_range(int v, bool edge = false) const {
check_vertex(v);
return {tin[v] + (edge ? 1 : 0), tout[v]};
}
std::vector<int> subtree_vertices(int v) const {
check_vertex(v);
return std::vector<int>(order.begin() + tin[v], order.begin() + tout[v]);
}
template <class F>
void for_each_subtree(int v, F f) const {
auto [l, r] = subtree_range(v);
for (int i = l; i < r; ++i) f(order[i]);
}
};
} // namespace tree
} // namespace m1une