-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBlockCutTree.cpp
More file actions
71 lines (60 loc) · 2.12 KB
/
BlockCutTree.cpp
File metadata and controls
71 lines (60 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
struct BlockCutTree {
vector<vector<int>> g, tree, comp;
vector<int> id, cut;
BlockCutTree(int n) : n(n), g(n), cut(n) {}
void addEdge(int u, int v){
g[u].emplace_back(v);
g[v].emplace_back(u);
}
void build(){
pre = low = id = vector<int>(n, -1);
for(int u=0; u<n; u++, chd=0) if(pre[u] == -1) //if graph is disconected
tarjan(u, -1), makeComp(-1); //find cut vertex and make components
for(int u=0; u<n; u++) if(cut[u]) comp.emplace_back(1, u); //create cut components
for(int i=0; i<comp.size(); i++) //mark id of each node
for(auto u : comp[i]) id[u] = i;
tree.resize(comp.size());
for(int i=0; i<comp.size(); i++)
for(auto u : comp[i]) if(id[u] != i)
tree[i].push_back(id[u]),
tree[id[u]].push_back(i);
}
private:
vector<int> pre, low;
vector<pair<int, int>> st;
int n, clk = 0, chd=0, ct, a, b;
void makeComp(int u){
comp.emplace_back();
do {
tie(a, b) = st.back();
st.pop_back();
comp.back().push_back(b);
} while(a != u);
if(~u) comp.back().push_back(u);
}
void tarjan(int u, int p){
pre[u] = low[u] = clk++;
st.emplace_back(p, u);
for(auto v : g[u]) if(v != p){
if(pre[v] == -1){
tarjan(v, u);
low[u] = min(low[u], low[v]);
cut[u] |= ct = (~p && low[v] >= pre[u]) || (p==-1 && ++chd >= 2);
if(ct) makeComp(u);
}
else low[u] = min(low[u], pre[v]);
}
}
};
/*LATEX_DESC_BEGIN***************************
**Block Cut Tree - BiConnected Component**
BlockCutTree bcc(n);
bcc.addEdge(u, v);
bcc.build();
bcc.tree -> graph of BlockCutTree (tree.size() <= 2n)
bcc.id[u] -> componet of u in the tree
bcc.cut[u] -> 1 if u is a cut vertex; 0 otherwise
bcc.comp[i] -> vertex of comp i (cut are part of multiple comp)
*****************************LATEX_DESC_END*/