Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- /*
- all nodes are completely renamed
- BC contains the block-cut tree
- NN is total nodes in BCT
- u in original graph is in node[u] in BCT
- if ap[u] == true, then u is articulation point in original graph
- if cut[u] == true, then u is a cut node in BCT, else this represents a biconnected component
- */
- const int M=100010;
- int disc[M], low[M], parent[M], ap[M];
- int node[M], cut[M];
- vector<int> A[M], BC[M];
- stack< pair<int, int> >_stack;
- vector<int> _temp;
- int NN;
- void extract_biconnected_component(int u = -1, int v = -1)
- {
- int id = ++NN;
- _temp.clear();
- while (!_stack.empty())
- {
- pair<int, int> t = _stack.top();
- _stack.pop();
- _temp.push_back(t.first), _temp.push_back(t.second);
- if (t == make_pair(u, v)) break;
- }
- sort(_temp.begin(), _temp.end());
- _temp.erase(unique(_temp.begin(), _temp.end()), _temp.end());
- for (int i=0; i<_temp.size(); i++)
- {
- int u = _temp[i];
- if (ap[u])
- {
- if (node[u] == -1) node[u] = ++NN;
- cut[node[u]] = true;
- BC[node[u]].push_back(id);
- BC[id].push_back(node[u]);
- }
- else node[u] = id;
- }
- }
- void dfs_visit(int u)
- {
- static int time = 0;
- int children = 0;
- disc[u] = low[u] = ++time;
- for (int i=0; i<A[u].size(); i++)
- {
- int v = A[u][i];
- if (disc[v] == -1)
- {
- _stack.push(make_pair(u, v));
- children++;
- parent[v] = u;
- dfs_visit(v);
- low[u] = min(low[u], low[v]);
- if ((parent[u] == -1 && children > 1) || (parent[u] != -1 && low[v] >= disc[u]))
- {
- ap[u] = true;
- extract_biconnected_component(u, v);
- }
- }
- else if (v != parent[u] && disc[v] < disc[u])
- {
- _stack.push(make_pair(u, v));
- low[u] = min(low[u], disc[v]);
- }
- }
- if (parent[u] == -1) extract_biconnected_component();
- }
- void block_cut(int n)
- {
- int i, j, k;
- NN = 0;
- for (i=0; i<=n; i++) {
- disc[i] = -1;
- ap[i] = 0;
- parent[i] = -1;
- node[i] = -1;
- cut[i] = 0;
- }
- for (i=1; i<=n; i++) if (disc[i] == -1) dfs_visit(i);
- }
- int main()
- {
- int n, m, i, j, k;
- cin>>n>>m;
- while (m--)
- {
- cin>>i>>j;
- A[i].push_back(j);
- A[j].push_back(i);
- }
- block_cut(n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement