説明

二辺連結成分分解とも。二重辺連結成分(2-edge connected component) とは, $1$ 個の辺を取り除いても連結である部分グラフである。つまり橋を含まないような部分グラフなので, 橋を列挙することで二重辺連結成分を列挙できる。

計算量

  • $O(E + V)$

実装例

依存ライブラリ LowLink

  • TwoEdgeConnectedComponents($g$):= グラフ $g$ で初期化する。
  • build($t$):= 二重辺連結成分分解をする。$t$ には二重辺連結成分ごとに縮約したグラフが格納される(橋のみからなるグラフ)。
  • [$k$]:= 頂点 $k$ が属する二重辺連結成分の番号を返す。
template< typename G >
struct TwoEdgeConnectedComponents : LowLink< G > {
  using LL = LowLink< G >;
  vector< int > comp;
 
  TwoEdgeConnectedComponents(const G &g) : LL(g) {}
 
  int operator[](const int &k) {
    return comp[k];
  }
 
  void dfs(int idx, int par, int &k) {
    if(~par && this->ord[par] >= this->low[idx]) comp[idx] = comp[par];
    else comp[idx] = k++;
    for(auto &to : this->g[idx]) {
      if(comp[to] == -1) dfs(to, idx, k);
    }
  }
 
  void build(UnWeightedGraph &t) {
    LL::build();
    comp.assign(this->g.size(), -1);
    int k = 0;
    for(int i = 0; i < comp.size(); i++) {
      if(comp[i] == -1) dfs(i, -1, k);
    }
    t.resize(k);
    for(auto &e : this->bridge) {
      int x = comp[e.first], y = comp[e.second];
      t[x].push_back(y);
      t[y].push_back(x);
    }
  }
};

検証

AtCoder Regular Contest 039 - D 旅行会社高橋君

template< typename G >
struct DoublingLowestCommonAncestor {
  const int LOG;
  vector< int > dep;
  const G &g;
  vector< vector< int > > table;
 
  DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
    table.assign(LOG, vector< int >(g.size(), -1));
  }
 
  void dfs(int idx, int par, int d) {
    table[0][idx] = par;
    dep[idx] = d;
    for(auto &to : g[idx]) {
      if(to != par) dfs(to, idx, d + 1);
    }
  }
 
  void build() {
    dfs(0, -1, 0);
    for(int k = 0; k + 1 < LOG; k++) {
      for(int i = 0; i < table[k].size(); i++) {
        if(table[k][i] == -1) table[k + 1][i] = -1;
        else table[k + 1][i] = table[k][table[k][i]];
      }
    }
  }
 
  int query(int u, int v) {
    if(dep[u] > dep[v]) swap(u, v);
    for(int i = LOG - 1; i >= 0; i--) {
      if(((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
    }
    if(u == v) return u;
    for(int i = LOG - 1; i >= 0; i--) {
      if(table[i][u] != table[i][v]) {
        u = table[i][u];
        v = table[i][v];
      }
    }
    return table[0][u];
  }
};
 
int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  UnWeightedGraph g(N);
  for(int i = 0; i < M; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    --x, --y;
    g[x].emplace_back(y);
    g[y].emplace_back(x);
  }
  TwoEdgeConnectedComponents< UnWeightedGraph > bcc(g);
  UnWeightedGraph t;
  bcc.build(t);
  DoublingLowestCommonAncestor< UnWeightedGraph > lca(t);
  lca.build();
  int Q;
  scanf("%d", &Q);
  auto getdist = [&](int x, int y) {
    return lca.dep[x] + lca.dep[y] - 2 * lca.dep[lca.query(x, y)];
  };
  while(Q--) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    a = bcc[--a], b = bcc[--b], c = bcc[--c];
    if(getdist(a, c) == getdist(a, b) + getdist(b, c)) puts("OK");
    else puts("NG");
  }
}