Asa's CP Library

a01sa01to's competitive programming library.

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

View the Project on GitHub a01sa01to/cp-library

:heavy_check_mark: library/_internal/graph/connection.hpp

Depends on

Required by

Verified with

Code

#pragma once

#include <functional>
#include <vector>
using namespace std;

#include "../../data-structure/graph.hpp"

namespace asalib {
  namespace _internal {
    namespace graph {
      bool is_connected(const adjlist_t& adjlist) {
        vector<bool> visited(adjlist.size(), false);
        function<void(size_t)> dfs = [&](size_t v) {
          visited[v] = true;
          for (const auto& [u, _] : adjlist[v])
            if (!visited[u]) dfs(u);
        };
        dfs(0);
        return all_of(visited.begin(), visited.end(), [](bool v) {
          return v;
        });
      }
    }  // namespace graph
  }  // namespace _internal
}  // namespace asalib
#line 2 "library/_internal/graph/connection.hpp"

#include <functional>
#include <vector>
using namespace std;

#line 2 "library/data-structure/graph.hpp"

#include <cassert>
#include <optional>
#line 6 "library/data-structure/graph.hpp"
using namespace std;

#line 2 "library/_internal/graph-base.hpp"

#include <concepts>
#include <type_traits>
using namespace std;

namespace asalib {
  namespace _internal {
    class graph_base {};
    class notweighted_graph_base: public graph_base {};
    class weighted_graph_base: public graph_base {};

    template<typename T>
    concept is_graph = is_base_of_v<graph_base, T>;

    template<typename T>
    concept notweighted_graph = is_base_of_v<notweighted_graph_base, T>;

    template<typename T>
    concept weighted_graph = is_base_of_v<weighted_graph_base, T>;

    using adjlist_t = vector<vector<pair<size_t, size_t>>>;
    using edgelist_t = vector<pair<size_t, size_t>>;
  }  // namespace _internal
}  // namespace asalib
#line 9 "library/data-structure/graph.hpp"

namespace asalib {
  namespace graph {
    class graph: public _internal::notweighted_graph_base {
      public:
      graph(): n_vertex(0), n_edge(0) {}
      explicit graph(size_t n_vertex): n_vertex(n_vertex), n_edge(0) {
        adj_list.reserve(n_vertex);
        adj_list.resize(n_vertex);
      }

      void add_edge(size_t v1, size_t v2) {
        assert(0 <= v1 && v1 < n_vertex);
        assert(0 <= v2 && v2 < n_vertex);
        adj_list[v1].push_back({ v2, n_edge });
        adj_list[v2].push_back({ v1, n_edge });
        edge_list.push_back({ v1, v2 });
        ++n_edge;
      }

      // (v1, v2)
      pair<size_t, size_t> get_edge(size_t edgeId) const {
        assert(0 <= edgeId && edgeId < n_edge);
        return edge_list[edgeId];
      }

      // (v2, edgeId)
      vector<pair<size_t, size_t>> get_adj(size_t vertex) const {
        assert(0 <= vertex && vertex < n_vertex);
        return adj_list[vertex];
      }

      // ---------- prototype ---------- //
      optional<pair<vector<size_t>, vector<size_t>>> cycle() const;
      bool is_connected() const;
      optional<pair<vector<size_t>, vector<size_t>>> eulerian_walk() const;

      private:
      size_t n_vertex, n_edge;
      asalib::_internal::adjlist_t adj_list;
      asalib::_internal::edgelist_t edge_list;
    };
  }  // namespace graph
}  // namespace asalib
#line 8 "library/_internal/graph/connection.hpp"

namespace asalib {
  namespace _internal {
    namespace graph {
      bool is_connected(const adjlist_t& adjlist) {
        vector<bool> visited(adjlist.size(), false);
        function<void(size_t)> dfs = [&](size_t v) {
          visited[v] = true;
          for (const auto& [u, _] : adjlist[v])
            if (!visited[u]) dfs(u);
        };
        dfs(0);
        return all_of(visited.begin(), visited.end(), [](bool v) {
          return v;
        });
      }
    }  // namespace graph
  }  // namespace _internal
}  // namespace asalib
Back to top page