library_cpp

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub hidehic0/library_cpp

:heavy_check_mark: graph/lowlink.hpp

Depends on

Verified with

Code

#pragma once
#include "templates/macro.hpp"
#include <vector>

// 参考: https://algo-logic.info/bridge-lowlink/
struct LowLink {
  std::vector<int> ord, low, parents, articulation_points;
  std::vector<std::vector<int>> G;
  std::vector<bool> used;
  std::vector<std::pair<int, int>> bridges;
  LowLink(const std::vector<std::vector<int>> &_G) : G(_G) {
    ord.resize(_G.size(), 0), low.resize(_G.size(), 0),
        used.resize(_G.size(), false), parents.resize(_G.size(), -1);
    int d = 0;
    rep(i, _G.size()) {
      if (!used[i])
        d = dfs(i, d, -1);
    }
  }

  int dfs(int cur, int d, int par = -1) {
    used[cur] = true, ord[cur] = low[cur] = d++, parents[cur] = par;
    bool is_articulation_point = false;
    int child_cnt = 0;

    for (const auto &nxt : G[cur]) {
      if (!used[nxt]) {
        child_cnt++, d = dfs(nxt, d, cur);
        chmin(low[cur], low[nxt]);
        if (par != -1 && ord[cur] <= low[nxt])
          is_articulation_point = true;
        if (ord[cur] < low[nxt])
          bridges.emplace_back(cur, nxt);
      } else if (nxt != par) {
        chmin(low[cur], ord[nxt]);
      }
    }

    if (par == -1 && child_cnt >= 2)
      is_articulation_point = true;
    if (is_articulation_point)
      articulation_points.emplace_back(cur);

    return d;
  }
};
#line 2 "templates/macro.hpp"
#include <iostream>
#include <vector>

// 引数の長さで内容が変わるrep 参考: https://trap.jp/post/1224
#define overload4(a, b, c, d, ...) d
#define _rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) overload4(__VA_ARGS__, REP, _rep)(__VA_ARGS__)
#define _rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define RREP(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); i--)
#define rrep(...) overload4(__VA_ARGS__, RREP, _rrep)(__VA_ARGS__)

#define all(a) (a).begin(), (a).end()

template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 == v.size() ? "" : " ");
  }
  return os;
}

// pythonのprintライクな関数 参考:
// https://nyaannyaan.github.io/library/template/inout.hpp
inline void out() { std::cout << std::endl; }
template <typename T, typename... U, char sep = ' '>
void out(const T &t, const U &...u) {
  std::cout << t;

  if (sizeof...(u))
    std::cout << sep;

  out(u...);
}

// cinの短縮関数 参考: https://nyaannyaan.github.io/library/template/inout.hpp
inline void in() {}
template <typename T, class... U> void in(T &t, U &...u) {
  std::cin >> t;
  in(u...);
}

template <typename T> inline T ceil_div(T a, T b) { return (a + b - 1) / b; }
template <typename T> inline T mod_pow(T a, T n, T mod) {
  T res = 1;
  while (n) {
    if (n % 2 != 0) {
      res *= a;
      res %= mod;
    }

    a *= a;
    a %= mod;

    n >>= 1;
  }

  return res;
}
template <typename T> inline T minus_mod(T a, T b) { return ((a % b) + b) % b; }

template <typename T> void apply_vec(std::vector<T> &v, T (*fn)(T)) {
  for (int i = 0; i < v.size(); i++)
    v[i] = fn(v[i]);
}
#line 4 "graph/lowlink.hpp"

// 参考: https://algo-logic.info/bridge-lowlink/
struct LowLink {
  std::vector<int> ord, low, parents, articulation_points;
  std::vector<std::vector<int>> G;
  std::vector<bool> used;
  std::vector<std::pair<int, int>> bridges;
  LowLink(const std::vector<std::vector<int>> &_G) : G(_G) {
    ord.resize(_G.size(), 0), low.resize(_G.size(), 0),
        used.resize(_G.size(), false), parents.resize(_G.size(), -1);
    int d = 0;
    rep(i, _G.size()) {
      if (!used[i])
        d = dfs(i, d, -1);
    }
  }

  int dfs(int cur, int d, int par = -1) {
    used[cur] = true, ord[cur] = low[cur] = d++, parents[cur] = par;
    bool is_articulation_point = false;
    int child_cnt = 0;

    for (const auto &nxt : G[cur]) {
      if (!used[nxt]) {
        child_cnt++, d = dfs(nxt, d, cur);
        chmin(low[cur], low[nxt]);
        if (par != -1 && ord[cur] <= low[nxt])
          is_articulation_point = true;
        if (ord[cur] < low[nxt])
          bridges.emplace_back(cur, nxt);
      } else if (nxt != par) {
        chmin(low[cur], ord[nxt]);
      }
    }

    if (par == -1 && child_cnt >= 2)
      is_articulation_point = true;
    if (is_articulation_point)
      articulation_points.emplace_back(cur);

    return d;
  }
};
Back to top page