cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: グラフ/橋の列挙
(test/graph/enumerate_bridges.test.cpp)

Depends on

Code

/*
 * @title グラフ/橋の列挙
 *
 * verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_3_B
 */

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

#include "emthrm/graph/edge.hpp"
#include "emthrm/graph/enumerate_bridges.hpp"

int main() {
  int v, e;
  std::cin >> v >> e;
  std::vector<std::vector<emthrm::Edge<bool>>> graph(v);
  while (e--) {
    int s, t;
    std::cin >> s >> t;
    graph[s].emplace_back(s, t);
    graph[t].emplace_back(t, s);
  }
  std::vector<emthrm::Edge<bool>> bridges = emthrm::enumerate_bridges(graph);
  std::ranges::sort(bridges.begin(), bridges.end(), {},
                    [](const emthrm::Edge<bool>& e) -> std::pair<int, int> {
                      return std::make_pair(e.src, e.dst);
                    });
  for (const emthrm::Edge<bool>& bridge : bridges) {
    std::cout << bridge.src << ' ' << bridge.dst << '\n';
  }
  return 0;
}
#line 1 "test/graph/enumerate_bridges.test.cpp"
/*
 * @title グラフ/橋の列挙
 *
 * verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_3_B
 */

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

#line 1 "include/emthrm/graph/edge.hpp"
/**
 * @title 辺
 */

#ifndef EMTHRM_GRAPH_EDGE_HPP_
#define EMTHRM_GRAPH_EDGE_HPP_

#include <compare>

namespace emthrm {

template <typename CostType>
struct Edge {
  CostType cost;
  int src, dst;

  explicit Edge(const int src, const int dst, const CostType cost = 0)
      : cost(cost), src(src), dst(dst) {}

  auto operator<=>(const Edge& x) const = default;
};

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_EDGE_HPP_
#line 1 "include/emthrm/graph/enumerate_bridges.hpp"



#line 6 "include/emthrm/graph/enumerate_bridges.hpp"

#line 1 "include/emthrm/graph/edge.hpp"
/**
 * @title 辺
 */

#ifndef EMTHRM_GRAPH_EDGE_HPP_
#define EMTHRM_GRAPH_EDGE_HPP_

#include <compare>

namespace emthrm {

template <typename CostType>
struct Edge {
  CostType cost;
  int src, dst;

  explicit Edge(const int src, const int dst, const CostType cost = 0)
      : cost(cost), src(src), dst(dst) {}

  auto operator<=>(const Edge& x) const = default;
};

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_EDGE_HPP_
#line 8 "include/emthrm/graph/enumerate_bridges.hpp"

namespace emthrm {

template <typename CostType>
std::vector<Edge<CostType>> enumerate_bridges(
    const std::vector<std::vector<Edge<CostType>>>& graph) {
  const int n = graph.size();
  std::vector<Edge<CostType>> res;
  std::vector<int> depth(n, -1), imos(n, 0);
  const auto dfs = [&graph, &res, &depth, &imos](
      auto dfs, const int par, const int ver) -> void {
    bool has_multiple_edges = false;
    for (const Edge<CostType>& e : graph[ver]) {
      if (depth[e.dst] == -1) {
        depth[e.dst] = depth[ver] + 1;
        dfs(dfs, ver, e.dst);
        if (imos[e.dst] == 0) {
          res.emplace_back(std::min(ver, e.dst), std::max(ver, e.dst), e.cost);
        }
        imos[ver] += imos[e.dst];
      } else if (!has_multiple_edges && e.dst == par) {
        has_multiple_edges = true;
      } else if (depth[e.dst] < depth[ver]) {
        ++imos[ver];
        --imos[e.dst];
      }
    }
  };
  for (int i = 0; i < n; ++i) {
    if (depth[i] == -1) {
      depth[i] = 0;
      dfs(dfs, -1, i);
    }
  }
  return res;
}

}  // namespace emthrm


#line 14 "test/graph/enumerate_bridges.test.cpp"

int main() {
  int v, e;
  std::cin >> v >> e;
  std::vector<std::vector<emthrm::Edge<bool>>> graph(v);
  while (e--) {
    int s, t;
    std::cin >> s >> t;
    graph[s].emplace_back(s, t);
    graph[t].emplace_back(t, s);
  }
  std::vector<emthrm::Edge<bool>> bridges = emthrm::enumerate_bridges(graph);
  std::ranges::sort(bridges.begin(), bridges.end(), {},
                    [](const emthrm::Edge<bool>& e) -> std::pair<int, int> {
                      return std::make_pair(e.src, e.dst);
                    });
  for (const emthrm::Edge<bool>& bridge : bridges) {
    std::cout << bridge.src << ' ' << bridge.dst << '\n';
  }
  return 0;
}
Back to top page