Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- stack<pair<int, int> > BCC_stack;
- void BCCUtil(vector<int> adj[], int u, bool visited[],
- int disc[], int low[], int& time, int parent)
- {
- // Mark the current node as visited
- visited[u] = true;
- // Initialize discovery time and low value
- disc[u] = low[u] = ++time;
- // Go through all vertices adjacent to this
- for (auto v : adj[u]) {
- // If v is not visited yet, then make it a child of u
- // in DFS tree and recur for it
- if (!visited[v]) {
- BCC_stack.push(make_pair(u, v)); // add edge to the stack
- BCCUtil(adj, v, visited, disc, low, time, u, isAP);
- // Check if the subtree rooted with v has
- // a connection to one of the ancestors of u
- low[u] = min(low[u], low[v]);
- // If u is not root and low value of one of
- // its child is more than discovery value of u.
- if (parent != -1 && low[v] >= disc[u]) {
- // pop values from stack until we see the (u, v) edge and those edges are a BCC
- vector<pair<int, int> > current_component;
- pair<int, int> value = BCC_stack.top();
- BCC_stack.pop();
- current_component.push_back(value);
- while (value != make_pair(u, v)) {
- value = BCC_stack.top();
- BCC_stack.pop();
- current_component.push_back(value);
- }
- }
- if (parent == -1) { // we are the root node
- // pop values from stack until we see the (u, v) edge and those edges are a BCC
- vector<pair<int, int> > current_component;
- pair<int, int> value = BCC_stack.top();
- BCC_stack.pop();
- current_component.push_back(value);
- while (value != make_pair(u, v)) {
- value = BCC_stack.top();
- BCC_stack.pop();
- current_component.push_back(value);
- }
- }
- }
- // Update low value of u for parent function calls.
- else if (v != parent)
- low[u] = min(low[u], disc[v]);
- }
- }
Advertisement
Advertisement