-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathBCC.cpp
76 lines (63 loc) · 1.71 KB
/
BCC.cpp
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
72
73
74
75
76
/**
* Team Proof
* BiConnected Components
*
* Fill edgeList, deg, from, to, cost
*/
#include<algorithm>
#include<cstring>
using namespace std;
const int maxNode = 25;
const int maxEdge = 25;
int adjList [maxNode][maxNode]; // adjList[i][j] is number of jth edge coming out of i
int deg[maxNode];
int from[maxEdge], to[maxEdge], cost[maxEdge];
int dfsNum[maxNode], minDfsNum[maxNode], parent[maxNode]; // parent is useful only in undirected graphs
int currentComp[maxEdge], component[maxEdge], compSize[maxEdge];
bool inComp[maxEdge];
int dfsNext, currentSize, totalBCC;
void dfs ( int v)
{
dfsNum[v] = minDfsNum[v] = dfsNext++;
for(int i = 0;i < deg[v]; i++)
{
int e = adjList[v][i];
int w = ( to[e] == v ? from[e] : to[e] );
if(w == parent[v]) continue;
if ( dfsNum[w] < 0 ) // W was a fresh vertex hence a proper ancestor of v
{
currentComp[currentSize++] = e;
inComp[e] = true;
parent[w] = v; // We don't need notion of parent for directed graphs
dfs(w);
if( minDfsNum[w] >= dfsNum[v] ) // New biconnected compnent found. Pop all edges until ( v, w)
{
while(true)
{
int edge = currentComp[--currentSize];
inComp[edge] = false;
component[edge] = totalBCC;
compSize[totalBCC]++;
if( edge == e) break;
}
totalBCC++;
}
else
minDfsNum[v] = min ( minDfsNum[v], minDfsNum[w] );
}
else if ( dfsNum[w] < dfsNum[v]) // Back edge found
{
currentComp[currentSize++] = e;
inComp[e] = true;
minDfsNum[v] = min ( minDfsNum[v], dfsNum[w] );
}
}
}
void bcc()
{
memset( dfsNum, -1, sizeof dfsNum);
memset( inComp, false, sizeof inComp);
memset( compSize, 0, sizeof compSize);
totalBCC = dfsNext = currentSize = 0;
dfs(0);
}