1
1
#include < bits/stdc++.h>
2
2
using namespace std ;
3
- #define pii pair<int ,int >
4
3
5
4
struct BlockCutTree {
6
5
vector<vector<int >> g, tree, comp;
@@ -17,34 +16,21 @@ struct BlockCutTree {
17
16
for (int u=0 ; u<n; u++, chd=0 ) if (pre[u] == -1 ) // if graph is disconected
18
17
tarjan (u, -1 ), makeComp (-1 ); // find cut vertex and make components
19
18
20
- for (int i=0 , ct=1 ; i<comp.size (); i++, ct=1 ){ // remove components with only cuts
21
- for (auto u : comp[i]) ct &= cut[u];
22
- if (ct) swap (comp[i], comp.back ()), comp.pop_back (), i--;
23
- }
24
-
25
19
for (int u=0 ; u<n; u++) if (cut[u]) comp.emplace_back (1 , u); // create cut components
20
+ for (int i=0 ; i<comp.size (); i++) // mark id of each node
21
+ for (auto u : comp[i]) id[u] = i;
26
22
27
- for (int i=0 ; i<comp.size (); i++) // mark id of each node
28
- for (auto u : comp[i])
29
- id[u] = i;
30
-
31
- tree.resize (comp.size ()); // creates tree (looking only edges from cut points)
32
- pre.assign (comp.size (), 1 );
33
-
34
- for (int u=0 ; u<n; u++) if (cut[u]){
35
- for (auto v : g[u]) if (pre[id[v]] && (!cut[v] || u<v)) addt (id[u], id[v]), pre[id[v]]=0 ;
36
- for (auto v : g[u]) pre[id[v]] = 1 ;
37
- }
23
+ tree.resize (comp.size ());
24
+ for (int i=0 ; i<comp.size (); i++)
25
+ for (auto u : comp[i]) if (id[u] != i)
26
+ tree[i].push_back (id[u]),
27
+ tree[id[u]].push_back (i);
38
28
}
39
29
private:
40
30
vector<int > pre, low;
41
- vector<pii > st;
31
+ vector<pair< int , int > > st;
42
32
int n, clk = 0 , chd=0 , ct, a, b;
43
- void addt (int u, int v){
44
- tree[u].push_back (v);
45
- tree[v].push_back (u);
46
- }
47
-
33
+
48
34
void makeComp (int u){
49
35
comp.emplace_back ();
50
36
do {
@@ -71,13 +57,14 @@ struct BlockCutTree {
71
57
}
72
58
};
73
59
60
+
74
61
/* LATEX_DESC_BEGIN***************************
75
62
Block Cut Tree - BiConnected Component
76
63
BlockCutTree bcc(n);
77
64
bcc.addEdge(u, v);
78
65
bcc.build();
79
66
80
- bcc.tree -> graph of BlockCutTree
67
+ bcc.tree -> graph of BlockCutTree (tree.size() <= 2n)
81
68
bcc.id[u] -> componet of u in the tree
82
69
bcc.cut[u] -> 1 if u is a cut vertex; 0 otherwise
83
70
bcc.comp[i] -> vertex of comp i (cut are part of multiple comp)
0 commit comments