cp_library

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

View the Project on GitHub SSRS-cp/cp_library

:heavy_check_mark: Block Cut Tree
(graph/block_cut_tree.hpp)

Depends on

Verified with

Code

#pragma once
/**
 * @brief Block Cut Tree
*/
#include "biconnected_components.hpp"
struct block_cut_tree{
  int V;
  vector<bool> art;
  vector<bool> cut;
  vector<vector<int>> G;
  vector<vector<int>> node;
  block_cut_tree(vector<vector<int>> &E){
    int N = E.size();
    int M = 0;
    vector<vector<pair<int, int>>> E2(N);
    for (int i = 0; i < N; i++){
      for (int j : E[i]){
        if (j > i){
          E2[i].push_back(make_pair(M, j));
          E2[j].push_back(make_pair(M, i));
          M++;
        }
      }
    }
    biconnected_components B(E2);
    art = vector<bool>(N, false);
    int cnt = 0;
    for (int i = 0; i < N; i++){
      for (auto P : E2[i]){
        if (B[P.first] != B[E2[i][0].first]){
          art[i] = true;
        }
      }
      if (art[i]){
        cnt++;
      }
      if (E[i].empty()){
        cnt++;
      }
    }
    V = cnt + B.cnt;
    cut = vector<bool>(V, false);
    G.resize(V);
    node.resize(V);
    int cnt2 = 0;
    vector<bool> used(B.cnt, false);
    for (int i = 0; i < N; i++){
      if (art[i]){
        cut[cnt2] = true;
        node[cnt2].push_back(i);
        for (auto P : E2[i]){
          int b = B[P.first];
          if (!used[b]){
            used[b] = true;
            G[cnt + b].push_back(cnt2);
            G[cnt2].push_back(cnt + b);
            node[cnt + b].push_back(i);
          }
        }
        for (auto P : E2[i]){
          int b = B[P.first];
          used[b] = false;
        }
        cnt2++;
      } else if (!E2[i].empty()){
        int b = B[E2[i][0].first];
        node[cnt + b].push_back(i);
      } else {
        node[cnt2].push_back(i);
        cnt2++;
      }
    }
  }
};
#line 2 "graph/block_cut_tree.hpp"
/**
 * @brief Block Cut Tree
*/
#line 2 "graph/biconnected_components.hpp"
/**
 * @brief 二重頂点連結成分分解
*/
struct biconnected_components{
  vector<int> bcc;
  int cnt = 0;
  biconnected_components(vector<vector<pair<int, int>>> &E){
    int N = E.size();
    vector<int> next(N, -1);
    vector<int> d(N, -1);
    vector<int> imos(N, 0);
    for (int i = 0; i < N; i++){
      if (d[i] == -1){
        d[i] = 0;
        dfs1(E, next, d, imos, i);
      }
    }
    int M = 0;
    for (int i = 0; i < N; i++){
      M += E[i].size();
    }
    M /= 2;
    bcc = vector<int>(M, -1);
    for (int i = 0; i < N; i++){
      if (d[i] == 0){
        dfs2(E, d, imos, cnt, i);
      }
    }
  }
  void dfs1(vector<vector<pair<int, int>>> &E, vector<int> &next, vector<int> &d, vector<int> &imos, int v){
    for (auto P : E[v]){
      int w = P.second;
      if (d[w] == -1){
        d[w] = d[v] + 1;
        next[v] = w;
        dfs1(E, next, d, imos, w);
        imos[v] += imos[w];
      } else if (d[w] < d[v] - 1){
        imos[v]++;
        imos[next[w]]--;
      }
    }
  }
  void dfs2(vector<vector<pair<int, int>>> &E, vector<int> &d, vector<int> &imos, int b, int v){
    for (auto P : E[v]){
      int x = P.first;
      int w = P.second;
      if (d[w] < d[v]){
        bcc[x] = b;
      } else if (d[w] == d[v] + 1 && bcc[x] == -1){
        if (imos[w] > 0){
          bcc[x] = b;
        } else {
          bcc[x] = cnt;
          cnt++;
        }
        dfs2(E, d, imos, bcc[x], w);
      }
    }
  }
  int operator [](int k){
    return bcc[k];
  }
};
#line 6 "graph/block_cut_tree.hpp"
struct block_cut_tree{
  int V;
  vector<bool> art;
  vector<bool> cut;
  vector<vector<int>> G;
  vector<vector<int>> node;
  block_cut_tree(vector<vector<int>> &E){
    int N = E.size();
    int M = 0;
    vector<vector<pair<int, int>>> E2(N);
    for (int i = 0; i < N; i++){
      for (int j : E[i]){
        if (j > i){
          E2[i].push_back(make_pair(M, j));
          E2[j].push_back(make_pair(M, i));
          M++;
        }
      }
    }
    biconnected_components B(E2);
    art = vector<bool>(N, false);
    int cnt = 0;
    for (int i = 0; i < N; i++){
      for (auto P : E2[i]){
        if (B[P.first] != B[E2[i][0].first]){
          art[i] = true;
        }
      }
      if (art[i]){
        cnt++;
      }
      if (E[i].empty()){
        cnt++;
      }
    }
    V = cnt + B.cnt;
    cut = vector<bool>(V, false);
    G.resize(V);
    node.resize(V);
    int cnt2 = 0;
    vector<bool> used(B.cnt, false);
    for (int i = 0; i < N; i++){
      if (art[i]){
        cut[cnt2] = true;
        node[cnt2].push_back(i);
        for (auto P : E2[i]){
          int b = B[P.first];
          if (!used[b]){
            used[b] = true;
            G[cnt + b].push_back(cnt2);
            G[cnt2].push_back(cnt + b);
            node[cnt + b].push_back(i);
          }
        }
        for (auto P : E2[i]){
          int b = B[P.first];
          used[b] = false;
        }
        cnt2++;
      } else if (!E2[i].empty()){
        int b = B[E2[i][0].first];
        node[cnt + b].push_back(i);
      } else {
        node[cnt2].push_back(i);
        cnt2++;
      }
    }
  }
};
Back to top page