Luzhiled's Library

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

View the Project on GitHub ei1333/library

:heavy_check_mark: Bipartite Graph Edge Coloring(二部グラフの辺彩色)
(graph/others/bipartite-graph-edge-coloring.hpp)

概要

二部グラフの辺彩色を求めるアルゴリズム. 辺彩色とは, 与えられたグラフの辺に色を付けることを指す. このとき隣接する(頂点を共有する)辺は全て異なる色になるようにする.

一般グラフの辺彩色数は, そのグラフの最大次数 $D$ または $D+1$ に一致する. 特に, 二部グラフに限定すると, その辺彩色数は 最大次数 $D$ に一致する.

まず, 与えられたグラフに対して最大次数 $D$ を維持したまま辺を追加したり縮約したりすることで$D-$正則グラフにする. $D$ が偶数の場合, オイラー閉路を求めて, 偶数番目に通った辺, 奇数番目に通った辺に分けて, 再帰的に解く. $D$ が奇数の場合, 完全マッチングを求めてそれらの辺を同じ色で塗った上で消して, 再帰的に解く. これらを繰り返すことで実際に $D$ 色の辺彩色を構成できる.

使い方

計算量

$O(M \sqrt N \log D)$

$N$: 頂点数, $M$: 辺の本数, $D$: 頂点の最大次数

Depends on

Verified with

Code

#pragma once

#include "../../structure/union-find/union-find.hpp"
#include "../flow/bipartite-flow.hpp"
#include "eulerian-trail.hpp"

/**
 * @brief Bipartite Graph Edge Coloring(二部グラフの辺彩色)
 * @docs docs/bipartite-graph-edge-coloring.md
 * @see https://ei1333.hateblo.jp/entry/2020/08/25/015955
 */
struct BipariteGraphEdgeColoring {
private:
  vector< vector< int > > ans;
  vector< int > A, B;
  int L, R;

  struct RegularGraph {
    int k{}, n{};
    vector< int > A, B;
  };
  RegularGraph g;

  static UnionFind contract(valarray< int > &deg, int k) {
    using pi = pair< int, int >;
    priority_queue< pi, vector< pi >, greater<> > que;
    for(int i = 0; i < (int) deg.size(); i++) {
      que.emplace(deg[i], i);
    }
    UnionFind uf(deg.size());
    while(que.size() > 1) {
      auto p = que.top();
      que.pop();
      auto q = que.top();
      que.pop();
      if(p.first + q.first > k) continue;
      p.first += q.first;
      uf.unite(p.second, q.second);
      que.emplace(p);
    }
    return uf;
  }


  RegularGraph build_k_regular_graph() {
    valarray< int > deg[2];
    deg[0] = valarray< int >(L);
    deg[1] = valarray< int >(R);
    for(auto &p : A) deg[0][p]++;
    for(auto &p : B) deg[1][p]++;

    int k = max(deg[0].max(), deg[1].max());

    /* step 1 */
    UnionFind uf[2];
    uf[0] = contract(deg[0], k);
    uf[1] = contract(deg[1], k);

    vector< int > id[2];
    int ptr[] = {0, 0};
    id[0] = vector< int >(L);
    id[1] = vector< int >(R);
    for(int i = 0; i < L; i++) if(uf[0].find(i) == i) id[0][i] = ptr[0]++;
    for(int i = 0; i < R; i++) if(uf[1].find(i) == i) id[1][i] = ptr[1]++;

    /* step 2 */
    int N = max(ptr[0], ptr[1]);
    deg[0] = valarray< int >(N);
    deg[1] = valarray< int >(N);

    /* step 3 */
    vector< int > C, D;
    C.reserve(N * k);
    D.reserve(N * k);
    for(int i = 0; i < (int) A.size(); i++) {
      int u = id[0][uf[0].find(A[i])];
      int v = id[1][uf[1].find(B[i])];
      C.emplace_back(u);
      D.emplace_back(v);
      deg[0][u]++;
      deg[1][v]++;
    }
    int j = 0;
    for(int i = 0; i < N; i++) {
      while(deg[0][i] < k) {
        while(deg[1][j] == k) ++j;
        C.emplace_back(i);
        D.emplace_back(j);
        ++deg[0][i];
        ++deg[1][j];
      }
    }

    return {k, N, C, D};
  }

  void rec(const vector< int > &ord, int k) {
    if(k == 0) {
      return;
    } else if(k == 1) {
      ans.emplace_back(ord);
      return;
    } else if((k & 1) == 0) {
      EulerianTrail< false > et(g.n + g.n);
      for(auto &p : ord) et.add_edge(g.A[p], g.B[p] + g.n);
      auto paths = et.enumerate_eulerian_trail();
      vector< int > path;
      for(auto &ps : paths) {
        for(auto &e : ps) path.emplace_back(ord[e]);
      }
      vector< int > beet[2];
      for(int i = 0; i < (int) path.size(); i++) {
        beet[i & 1].emplace_back(path[i]);
      }
      rec(beet[0], k / 2);
      rec(beet[1], k / 2);
    } else {
      BipartiteFlow flow(g.n, g.n);
      for(auto &i : ord) flow.add_edge(g.A[i], g.B[i]);
      flow.max_matching();
      vector< int > beet;
      ans.emplace_back();
      for(auto &i : ord) {
        if(flow.match_l[g.A[i]] == g.B[i]) {
          flow.match_l[g.A[i]] = -1;
          ans.back().emplace_back(i);
        } else {
          beet.emplace_back(i);
        }
      }
      rec(beet, k - 1);
    }
  }

public:
  explicit BipariteGraphEdgeColoring() : L(0), R(0) {}

  void add_edge(int a, int b) {
    A.emplace_back(a);
    B.emplace_back(b);
    L = max(L, a + 1);
    R = max(R, b + 1);
  }

  vector< vector< int > > build() {
    g = build_k_regular_graph();
    vector< int > ord(g.A.size());
    iota(ord.begin(), ord.end(), 0);
    rec(ord, g.k);
    vector< vector< int > > res;
    for(int i = 0; i < (int) ans.size(); i++) {
      res.emplace_back();
      for(auto &j : ans[i]) if(j < (int)A.size()) res.back().emplace_back(j);
    }
    return res;
  }
};
#line 2 "graph/others/bipartite-graph-edge-coloring.hpp"

#line 2 "structure/union-find/union-find.hpp"

struct UnionFind {
  vector< int > data;

  UnionFind() = default;

  explicit UnionFind(size_t sz) : data(sz, -1) {}

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return false;
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return true;
  }

  int find(int k) {
    if(data[k] < 0) return (k);
    return data[k] = find(data[k]);
  }

  int size(int k) {
    return -data[find(k)];
  }

  bool same(int x, int y) {
    return find(x) == find(y);
  }

  vector< vector< int > > groups() {
    int n = (int) data.size();
    vector< vector< int > > ret(n);
    for(int i = 0; i < n; i++) {
      ret[find(i)].emplace_back(i);
    }
    ret.erase(remove_if(begin(ret), end(ret), [&](const vector< int > &v) {
      return v.empty();
    }), end(ret));
    return ret;
  }
};
#line 1 "graph/flow/bipartite-flow.hpp"
/**
 * @brief Bipartite Flow(二部グラフのフロー)
 * @docs docs/bipartite-flow.md
 */
struct BipartiteFlow {
  size_t n, m, time_stamp;
  vector< vector< int > > g, rg;
  vector< int > match_l, match_r, dist, used, alive;
  bool matched;

public:
  explicit BipartiteFlow(size_t n, size_t m) :
      n(n), m(m), time_stamp(0), g(n), rg(m), match_l(n, -1), match_r(m, -1), used(n), alive(n, 1), matched(false) {}

  void add_edge(int u, int v) {
    g[u].push_back(v);
    rg[v].emplace_back(u);
  }

  vector< pair< int, int > > max_matching() {
    matched = true;
    for(;;) {
      build_augment_path();
      ++time_stamp;
      int flow = 0;
      for(int i = 0; i < (int)n; i++) {
        if(match_l[i] == -1) flow += find_min_dist_augment_path(i);
      }
      if(flow == 0) break;
    }
    vector< pair< int, int > > ret;
    for(int i = 0; i < (int)n; i++) {
      if(match_l[i] >= 0) ret.emplace_back(i, match_l[i]);
    }
    return ret;
  }

  /* http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=3198 */
  void erase_edge(int a, int b) {
    if(match_l[a] == b) {
      match_l[a] = -1;
      match_r[b] = -1;
    }
    g[a].erase(find(begin(g[a]), end(g[a]), b));
    rg[b].erase(find(begin(rg[b]), end(rg[b]), a));
  }

  /* http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0334 */
  vector< pair< int, int > > lex_max_matching() {
    if(!matched) max_matching();
    for(auto &vs : g) sort(begin(vs), end(vs));
    vector< pair< int, int > > es;
    for(int i = 0; i < (int)n; i++) {
      if(match_l[i] == -1 || alive[i] == 0) {
        continue;
      }
      match_r[match_l[i]] = -1;
      match_l[i] = -1;
      ++time_stamp;
      find_augment_path(i);
      alive[i] = 0;
      es.emplace_back(i, match_l[i]);
    }
    return es;
  }

  vector< int > min_vertex_cover() {
    auto visited = find_residual_path();
    vector< int > ret;
    for(int i = 0; i < (int)(n + m); i++) {
      if(visited[i] ^ (i < (int)n)) {
        ret.emplace_back(i);
      }
    }
    return ret;
  }

  /* https://atcoder.jp/contests/utpc2013/tasks/utpc2013_11 */
  vector< int > lex_min_vertex_cover(const vector< int > &ord) {
    assert(ord.size() == n + m);
    auto res = build_risidual_graph();
    vector< vector< int > > r_res(n + m + 2);
    for(int i = 0; i < (int)(n + m + 2); i++) {
      for(auto &j : res[i]) r_res[j].emplace_back(i);
    }
    queue< int > que;
    vector< int > visited(n + m + 2, -1);
    auto expand_left = [&](int t) {
      if(visited[t] != -1) return;
      que.emplace(t);
      visited[t] = 1;
      while(!que.empty()) {
        int idx = que.front();
        que.pop();
        for(auto &to : r_res[idx]) {
          if(visited[to] != -1) continue;
          visited[to] = 1;
          que.emplace(to);
        }
      }
    };
    auto expand_right = [&](int t) {
      if(visited[t] != -1) return;
      que.emplace(t);
      visited[t] = 0;
      while(!que.empty()) {
        int idx = que.front();
        que.pop();
        for(auto &to : res[idx]) {
          if(visited[to] != -1) continue;
          visited[to] = 0;
          que.emplace(to);
        }
      }
    };
    expand_right(n + m);
    expand_left(n + m + 1);
    vector< int > ret;
    for(auto &t : ord) {
      if(t < (int)n) {
        expand_left(t);
        if(visited[t] & 1) ret.emplace_back(t);
      } else {
        expand_right(t);
        if(~visited[t] & 1) ret.emplace_back(t);
      }
    }
    return ret;
  }


  vector< int > max_independent_set() {
    auto visited = find_residual_path();
    vector< int > ret;
    for(int i = 0; i < (int)(n + m); i++) {
      if(visited[i] ^ (i >= (int)n)) {
        ret.emplace_back(i);
      }
    }
    return ret;
  }

  vector< pair< int, int > > min_edge_cover() {
    auto es = max_matching();
    for(int i = 0; i < (int)n; i++) {
      if(match_l[i] >= 0) {
        continue;
      }
      if(g[i].empty()) {
        return {};
      }
      es.emplace_back(i, g[i][0]);
    }
    for(int i = 0; i < (int)m; i++) {
      if(match_r[i] >= 0) {
        continue;
      }
      if(rg[i].empty()) {
        return {};
      }
      es.emplace_back(rg[i][0], i);
    }
    return es;
  }

  // left: [0,n), right: [n,n+m), S: n+m, T: n+m+1
  vector< vector< int > > build_risidual_graph() {
    if(!matched) max_matching();
    const size_t S = n + m;
    const size_t T = n + m + 1;
    vector< vector< int > > ris(n + m + 2);
    for(int i = 0; i < (int)n; i++) {
      if(match_l[i] == -1) ris[S].emplace_back(i);
      else ris[i].emplace_back(S);
    }
    for(int i = 0; i < (int)m; i++) {
      if(match_r[i] == -1) ris[i + n].emplace_back(T);
      else ris[T].emplace_back(i + n);
    }
    for(int i = 0; i < (int)n; i++) {
      for(auto &j : g[i]) {
        if(match_l[i] == j) ris[j + n].emplace_back(i);
        else ris[i].emplace_back(j + n);
      }
    }
    return ris;
  }

private:
  vector< int > find_residual_path() {
    auto res = build_risidual_graph();
    queue< int > que;
    vector< int > visited(n + m + 2);
    que.emplace(n + m);
    visited[n + m] = true;
    while(!que.empty()) {
      int idx = que.front();
      que.pop();
      for(auto &to : res[idx]) {
        if(visited[to]) continue;
        visited[to] = true;
        que.emplace(to);
      }
    }
    return visited;
  }

  void build_augment_path() {
    queue< int > que;
    dist.assign(g.size(), -1);
    for(int i = 0; i < (int)n; i++) {
      if(match_l[i] == -1) {
        que.emplace(i);
        dist[i] = 0;
      }
    }
    while(!que.empty()) {
      int a = que.front();
      que.pop();
      for(auto &b : g[a]) {
        int c = match_r[b];
        if(c >= 0 && dist[c] == -1) {
          dist[c] = dist[a] + 1;
          que.emplace(c);
        }
      }
    }
  }

  bool find_min_dist_augment_path(int a) {
    used[a] = time_stamp;
    for(auto &b : g[a]) {
      int c = match_r[b];
      if(c < 0 || (used[c] != (int)time_stamp && dist[c] == dist[a] + 1 && find_min_dist_augment_path(c))) {
        match_r[b] = a;
        match_l[a] = b;
        return true;
      }
    }
    return false;
  }

  bool find_augment_path(int a) {
    used[a] = time_stamp;
    for(auto &b : g[a]) {
      int c = match_r[b];
      if(c < 0 || (alive[c] == 1 && used[c] != (int)time_stamp && find_augment_path(c))) {
        match_r[b] = a;
        match_l[a] = b;
        return true;
      }
    }
    return false;
  }
};
#line 2 "graph/others/eulerian-trail.hpp"

#line 4 "graph/others/eulerian-trail.hpp"

/**
 * @brief Eulerian Trail(オイラー路)
 * @docs docs/eulerian-trail.md
 */
template< bool directed >
struct EulerianTrail {
  vector< vector< pair< int, int > > > g;
  vector< pair< int, int > > es;
  int M;
  vector< int > used_vertex, used_edge, deg;

  explicit EulerianTrail(int V) : g(V), M(0), used_vertex(V), deg(V) {}

  void add_edge(int a, int b) {
    es.emplace_back(a, b);
    g[a].emplace_back(b, M);
    if(directed) {
      deg[a]++;
      deg[b]--;
    } else {
      g[b].emplace_back(a, M);
      deg[a]++;
      deg[b]++;
    }
    M++;
  }

  pair< int, int > get_edge(int idx) const {
    return es[idx];
  }

  vector< vector< int > > enumerate_eulerian_trail() {
    if(directed) {
      for(auto &p : deg) if(p != 0) return {};
    } else {
      for(auto &p : deg) if(p & 1) return {};
    }
    used_edge.assign(M, 0);
    vector< vector< int > > ret;
    for(int i = 0; i < (int) g.size(); i++) {
      if(g[i].empty() || used_vertex[i]) continue;
      ret.emplace_back(go(i));
    }
    return ret;
  }

  vector< vector< int > > enumerate_semi_eulerian_trail() {
    UnionFind uf(g.size());
    for(auto &p : es) uf.unite(p.first, p.second);
    vector< vector< int > > group(g.size());
    for(int i = 0; i < (int) g.size(); i++) group[uf.find(i)].emplace_back(i);
    vector< vector< int > > ret;
    used_edge.assign(M, 0);
    for(auto &vs : group) {
      if(vs.empty()) continue;
      int latte = -1, malta = -1;
      if(directed) {
        for(auto &p : vs) {
          if(abs(deg[p]) > 1) {
            return {};
          } else if(deg[p] == 1) {
            if(latte >= 0) return {};
            latte = p;
          }
        }
      } else {
        for(auto &p : vs) {
          if(deg[p] & 1) {
            if(latte == -1) latte = p;
            else if(malta == -1) malta = p;
            else return {};
          }
        }
      }
      ret.emplace_back(go(latte == -1 ? vs.front() : latte));
      if(ret.back().empty()) ret.pop_back();
    }
    return ret;
  }

  vector< int > go(int s) {
    stack< pair< int, int > > st;
    vector< int > ord;
    st.emplace(s, -1);
    while(!st.empty()) {
      int idx = st.top().first;
      used_vertex[idx] = true;
      if(g[idx].empty()) {
        ord.emplace_back(st.top().second);
        st.pop();
      } else {
        auto e = g[idx].back();
        g[idx].pop_back();
        if(used_edge[e.second]) continue;
        used_edge[e.second] = true;
        st.emplace(e);
      }
    }
    ord.pop_back();
    reverse(ord.begin(), ord.end());
    return ord;
  }
};
#line 6 "graph/others/bipartite-graph-edge-coloring.hpp"

/**
 * @brief Bipartite Graph Edge Coloring(二部グラフの辺彩色)
 * @docs docs/bipartite-graph-edge-coloring.md
 * @see https://ei1333.hateblo.jp/entry/2020/08/25/015955
 */
struct BipariteGraphEdgeColoring {
private:
  vector< vector< int > > ans;
  vector< int > A, B;
  int L, R;

  struct RegularGraph {
    int k{}, n{};
    vector< int > A, B;
  };
  RegularGraph g;

  static UnionFind contract(valarray< int > &deg, int k) {
    using pi = pair< int, int >;
    priority_queue< pi, vector< pi >, greater<> > que;
    for(int i = 0; i < (int) deg.size(); i++) {
      que.emplace(deg[i], i);
    }
    UnionFind uf(deg.size());
    while(que.size() > 1) {
      auto p = que.top();
      que.pop();
      auto q = que.top();
      que.pop();
      if(p.first + q.first > k) continue;
      p.first += q.first;
      uf.unite(p.second, q.second);
      que.emplace(p);
    }
    return uf;
  }


  RegularGraph build_k_regular_graph() {
    valarray< int > deg[2];
    deg[0] = valarray< int >(L);
    deg[1] = valarray< int >(R);
    for(auto &p : A) deg[0][p]++;
    for(auto &p : B) deg[1][p]++;

    int k = max(deg[0].max(), deg[1].max());

    /* step 1 */
    UnionFind uf[2];
    uf[0] = contract(deg[0], k);
    uf[1] = contract(deg[1], k);

    vector< int > id[2];
    int ptr[] = {0, 0};
    id[0] = vector< int >(L);
    id[1] = vector< int >(R);
    for(int i = 0; i < L; i++) if(uf[0].find(i) == i) id[0][i] = ptr[0]++;
    for(int i = 0; i < R; i++) if(uf[1].find(i) == i) id[1][i] = ptr[1]++;

    /* step 2 */
    int N = max(ptr[0], ptr[1]);
    deg[0] = valarray< int >(N);
    deg[1] = valarray< int >(N);

    /* step 3 */
    vector< int > C, D;
    C.reserve(N * k);
    D.reserve(N * k);
    for(int i = 0; i < (int) A.size(); i++) {
      int u = id[0][uf[0].find(A[i])];
      int v = id[1][uf[1].find(B[i])];
      C.emplace_back(u);
      D.emplace_back(v);
      deg[0][u]++;
      deg[1][v]++;
    }
    int j = 0;
    for(int i = 0; i < N; i++) {
      while(deg[0][i] < k) {
        while(deg[1][j] == k) ++j;
        C.emplace_back(i);
        D.emplace_back(j);
        ++deg[0][i];
        ++deg[1][j];
      }
    }

    return {k, N, C, D};
  }

  void rec(const vector< int > &ord, int k) {
    if(k == 0) {
      return;
    } else if(k == 1) {
      ans.emplace_back(ord);
      return;
    } else if((k & 1) == 0) {
      EulerianTrail< false > et(g.n + g.n);
      for(auto &p : ord) et.add_edge(g.A[p], g.B[p] + g.n);
      auto paths = et.enumerate_eulerian_trail();
      vector< int > path;
      for(auto &ps : paths) {
        for(auto &e : ps) path.emplace_back(ord[e]);
      }
      vector< int > beet[2];
      for(int i = 0; i < (int) path.size(); i++) {
        beet[i & 1].emplace_back(path[i]);
      }
      rec(beet[0], k / 2);
      rec(beet[1], k / 2);
    } else {
      BipartiteFlow flow(g.n, g.n);
      for(auto &i : ord) flow.add_edge(g.A[i], g.B[i]);
      flow.max_matching();
      vector< int > beet;
      ans.emplace_back();
      for(auto &i : ord) {
        if(flow.match_l[g.A[i]] == g.B[i]) {
          flow.match_l[g.A[i]] = -1;
          ans.back().emplace_back(i);
        } else {
          beet.emplace_back(i);
        }
      }
      rec(beet, k - 1);
    }
  }

public:
  explicit BipariteGraphEdgeColoring() : L(0), R(0) {}

  void add_edge(int a, int b) {
    A.emplace_back(a);
    B.emplace_back(b);
    L = max(L, a + 1);
    R = max(R, b + 1);
  }

  vector< vector< int > > build() {
    g = build_k_regular_graph();
    vector< int > ord(g.A.size());
    iota(ord.begin(), ord.end(), 0);
    rec(ord, g.k);
    vector< vector< int > > res;
    for(int i = 0; i < (int) ans.size(); i++) {
      res.emplace_back();
      for(auto &j : ans[i]) if(j < (int)A.size()) res.back().emplace_back(j);
    }
    return res;
  }
};
Back to top page