CompetitiveProgrammingCpp

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

View the Project on GitHub

:heavy_check_mark: Library/Graph/Tree/EulerTour.hpp

Depends on

Verified with

Code

#pragma once

#include <stack>

#include <vector>


#include "../Graph.hpp"


namespace mtd {

  template <class Node, class Cost>
  class EulerTour {
    const std::vector<Node> m_tour;
    const std::vector<std::pair<int, int>> m_appear;

    auto constructTour(const Graph<Node, Cost>& tree, Node root) const {
      auto n = tree.size();
      std::vector<Node> tour;
      std::stack<Node> stk;
      std::vector<int> used(n);
      stk.emplace(root);
      while (!stk.empty()) {
        auto from = stk.top();
        tour.emplace_back(from);
        stk.pop();
        if (used[from]) { continue; }
        stk.emplace(from);
        used[from] = true;

        for (auto [to, _] : tree.getEdges(from)) {
          if (!used[to]) { stk.emplace(to); }
        }
      }
      return tour;
    }
    auto constructAppear(int n) const {
      std::vector<std::pair<int, int>> appear(n, {-1, -1});
      for (int i = 0; i < 2 * n; ++i) {
        if (appear[m_tour[i]].first == -1) {
          appear[m_tour[i]].first = i;
        } else {
          appear[m_tour[i]].second = i;
        }
      }
      return appear;
    }

  public:
    EulerTour(const Graph<Node, Cost>& tree, Node root = 0)
        : m_tour(constructTour(tree, root)),
          m_appear(constructAppear(tree.size())) {}

    auto lessOrder(int li, int ri) const {
      return m_appear[li].first < m_appear[ri].first;
    }
    auto isSon(Node son, Node parent) {
      return m_appear[parent].first < m_appear[son].first &&
             m_appear[son].second < m_appear[parent].second;
    }
    auto range(Node u) const { return m_appear[u]; }
  };
}  // namespace mtd
#line 2 "Library/Graph/Tree/EulerTour.hpp"

#include <stack>

#include <vector>


#line 2 "Library/Graph/Graph.hpp"
#include <deque>

#include <iostream>

#include <ranges>

#include <tuple>

#line 7 "Library/Graph/Graph.hpp"

namespace mtd {
  template <class Node = long long, class Cost = long long>
  class Graph {
    using Edge = std::pair<Node, Cost>;
    using Edges = std::vector<Edge>;

    const int m_n;
    std::vector<Edges> m_graph;

  public:
    Graph(int n) : m_n(n), m_graph(n) {}
    Graph(const std::vector<Edges>& edges)
        : m_n(edges.size()), m_graph(edges) {}
    Graph(int n, const std::vector<std::tuple<Node, Node>>& edges,
          bool is_arc = false, bool is_index1 = true)
        : Graph<Node, Cost>(n) {
      for (auto [u, v] : edges) {
        u -= is_index1;
        v -= is_index1;
        if (is_arc) {
          addArc(u, v);
        } else {
          addEdge(u, v);
        }
      }
    }
    Graph(int n, const std::vector<std::tuple<Node, Node, Cost>>& edges,
          bool is_arc = false, bool is_index1 = true)
        : Graph<Node, Cost>(n) {
      for (auto [u, v, c] : edges) {
        u -= is_index1;
        v -= is_index1;
        if (is_arc) {
          addArc(u, v, c);
        } else {
          addEdge(u, v, c);
        }
      }
    }

    auto addEdge(const Node& f, const Node& t, const Cost& c = 1) {
      addArc(f, t, c);
      addArc(t, f, c);
    }
    auto addArc(const Node& f, const Node& t, const Cost& c = 1) {
      m_graph[f].emplace_back(t, c);
    }
    auto getEdges(const Node& from) const {
      class EdgesRange {
        const typename Edges::const_iterator b, e;

      public:
        EdgesRange(const Edges& edges) : b(edges.begin()), e(edges.end()) {}
        auto begin() const { return b; }
        auto end() const { return e; }
      };
      return EdgesRange(m_graph[from]);
    }
    auto getEdges() const {
      std::deque<std::tuple<Node, Node, Cost>> edges;
      for (Node from : std::views::iota(0, m_n)) {
        for (const auto& [to, c] : getEdges(from)) {
          edges.emplace_back(from, to, c);
        }
      }
      return edges;
    }
    auto getEdgesExcludeCost() const {
      std::deque<std::pair<Node, Node>> edges;
      for (Node from : std::views::iota(0, m_n)) {
        for (const auto& [to, _] : getEdges(from)) {
          edges.emplace_back(from, to);
        }
      }
      return edges;
    }
    auto reverse() const {
      auto rev = Graph<Node, Cost>(m_n);
      for (const auto& [from, to, c] : getEdges()) { rev.addArc(to, from, c); }
      return rev;
    }
    auto size() const { return m_n; };
    auto debug(bool directed = false) const {
      for (const auto& [f, t, c] : getEdges()) {
        if (f < t || directed) {
          std::cout << f << " -> " << t << ": " << c << std::endl;
        }
      }
    }
  };
}  // namespace mtd

#line 7 "Library/Graph/Tree/EulerTour.hpp"

namespace mtd {

  template <class Node, class Cost>
  class EulerTour {
    const std::vector<Node> m_tour;
    const std::vector<std::pair<int, int>> m_appear;

    auto constructTour(const Graph<Node, Cost>& tree, Node root) const {
      auto n = tree.size();
      std::vector<Node> tour;
      std::stack<Node> stk;
      std::vector<int> used(n);
      stk.emplace(root);
      while (!stk.empty()) {
        auto from = stk.top();
        tour.emplace_back(from);
        stk.pop();
        if (used[from]) { continue; }
        stk.emplace(from);
        used[from] = true;

        for (auto [to, _] : tree.getEdges(from)) {
          if (!used[to]) { stk.emplace(to); }
        }
      }
      return tour;
    }
    auto constructAppear(int n) const {
      std::vector<std::pair<int, int>> appear(n, {-1, -1});
      for (int i = 0; i < 2 * n; ++i) {
        if (appear[m_tour[i]].first == -1) {
          appear[m_tour[i]].first = i;
        } else {
          appear[m_tour[i]].second = i;
        }
      }
      return appear;
    }

  public:
    EulerTour(const Graph<Node, Cost>& tree, Node root = 0)
        : m_tour(constructTour(tree, root)),
          m_appear(constructAppear(tree.size())) {}

    auto lessOrder(int li, int ri) const {
      return m_appear[li].first < m_appear[ri].first;
    }
    auto isSon(Node son, Node parent) {
      return m_appear[parent].first < m_appear[son].first &&
             m_appear[son].second < m_appear[parent].second;
    }
    auto range(Node u) const { return m_appear[u]; }
  };
}  // namespace mtd
Back to top page