Application of DFS Properties - Use Tarjan algorithm for cutting the top, BCC, SCC
Since finishing "algorithm contest entry classic training manual" as well as network
DFS (depth first search) depth-first search algorithm
Forest dfs: dfs execution order of all the edges of the graph to comb, into four categories: the front side, reverse side, and cross edge side of the tree. There is no cross-free edges to the graph, is equivalent to the forward side and the rear side.
pre value in the DFS process, u and their descendants can connect back to the earliest ancestors: low [u]
- Top cut: FIG For communication, so delete FIG no communication points .
- Bridge: FIG For communication, so delete FIG no communication side .
The method of calculating the cutting top: the DFS process, a point if there is one child node v u, v and their descendants that are not connected back to the reverse side of the ancestors of u (u is not included), i.e. lowv> = pre [u] , then u is cut top.
The method of calculation of the bridge: If you can only link back to the descendants of v v themselves (ie low (v)> pre (u)) is the uv bridge.
note:
1. For the access point has only the reverse side processing (condition pre [v] <pre [u ]).
2. For special sentenced to roots, when a child is not only cut the top, you need to manually cancel cut the top mark.
- No Twin connected component (points):, i.e., without any internal cutting edges in the top two in a simple ring.
- Undirected graph edges - bis connected components: all edges are not bridged, i.e. each edge are at least in a simple ring.
The method of calculation of the BCC: the edge of the stack, will be found after cutting the top edge of this side of the stack onto the stack, marker.
Method of calculating the edge-BCC: finding a bridge and then do not go through DFS to bridge.
- The strongly connected component: up to each point within the component
The method of calculating the SCC: the point stack, DFS recorded during lowlink, when lowlink (u) == pre (u) to the point where the stack tag.
note:
1. When you reach a point of access have been determined to ignore the number of points (conditions! Sccno [v])
2. a minimum value can take any (relatively small roots)
lowlinku = min(lowlinku, pre[v] OR lowlinkv);
Paste the code
BCC calculation
#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 2e4 + 1;
int dfs_clock, pre[maxn], iscut[maxn], bccno[maxn], bcc_count;
struct Edge {
int u, v;
};
vector <int> bcc[maxn], G[maxn];
stack <Edge> S;
void AddE(int u, int v) {
G[u].push_back(v);
}
int tarjan(int u, int fa) {
int lowu = pre[u] = ++dfs_clock, child = 0;
for (auto v: G[u]) {
if (!pre[v]) {
child++;
S.push((Edge) {u, v});
int lowv = tarjan(v, u);
lowu = min(lowu, lowv);
if (lowv >= pre[u]) {
iscut[u] = 1;
++bcc_count;
bcc[bcc_count].clear();
//cout<<dfs_clock<<' '<<u<<'>'<<v<<','<<lowv<<'|'<<bcc_count<<endl;
while (1) {
Edge e = S.top(); S.pop();
if (bccno[e.u] != bcc_count) {
bcc[bcc_count].push_back(e.u);
bccno[e.u] = bcc_count;
}
if (bccno[e.v] != bcc_count) {
bcc[bcc_count].push_back(e.v);
bccno[e.v] = bcc_count;
}
if (e.u == u && e.v == v) break;
}
}
} else if (pre[v] < pre[u] && v != fa) {
S.push((Edge) {u, v});
lowu = min(lowu, pre[v]); //Err
}
}
if (fa == -1 && child == 1) iscut[u] = 0; //Err
return lowu;
}
signed main() {
int n, m, u, v, i;
cin>>n>>m;
while (m--) {
cin>>u>>v;
AddE(u, v);
AddE(v, u);
}
for (i = 1; i <= n; ++i)
if (!pre[i])
tarjan(i, -1);
cout<<count(iscut + 1, iscut + n + 1, 1)<<endl;
for (i = 1; i <= n; ++i)
if (iscut[i])
cout<<i<<' ';
return 0;
}
Tarjan seeking the SCC + + condensing point Topological Sort
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
const int maxn = 1e4 + 1;
int n;
int pre[maxn], dfs_clock, sccno[maxn], scc_count;
int c[maxn << 1], topo[maxn << 1], cnt;
vector <int> G[maxn], GA[maxn], scc[maxn];
stack <int> S;
void AddE(int u, int v) {
G[u].push_back(v);
}
void AddED(int u, int v) {
GA[u].push_back(v);
}
int tarjan(int u) {
int lowlinku = pre[u] = ++dfs_clock;
S.push(u);
for (auto x: G[u])
if (!pre[x]) {
int lowlinkv = tarjan(x);
lowlinku = min(lowlinku, lowlinkv);
} else if (!sccno[x])
lowlinku = min(lowlinku, pre[x]);
if (lowlinku == pre[u]) {
++scc_count;
scc[scc_count].clear();
while (1) {
int v = S.top(); S.pop();
scc[scc_count].push_back(v);
sccno[v] = scc_count + n;
if (v == u) break;
}
}
return lowlinku;
}
int dfs(int u) {
static int cnt = n + scc_count;
if (c[u] == -1)
return false;
c[u] = -1;
for (auto v: GA[u])
if (!c[v] && !dfs(v)) return false;
c[u] = 1;
topo[cnt--] = u;
return true;
}
int toposort(int n) {
for (int i = 1; i <= n; ++i)
if (!c[i])
if (!dfs(i))
return false;
return true;
}
void sillys(int n) {
int ans = 0, cnt;
for (int i = n; i > 0; --i)
if (G[topo[i]].empty()) {
ans = topo[i];
cnt++;
}
if (cnt == 0)
cout<<"Fuck"<<endl;
else if (cnt > 1)
cout<<0<<endl;
else if (cnt == 1) {
if (ans <= n)
cout<<1<<endl;
else
cout<<scc[ans - ::n].size()<<endl;
}
return;
}
signed main() {
int m, i, u, v;
cin>>n>>m;
while (m--) {
cin>>u>>v;
AddE(u, v);
}
for (i = 1; i <= n; ++i) {
tarjan(i);
if (sccno[i] == 0)
sccno[i] = i;
}
for (i = 1; i <= n; ++i)
for (int v: G[i])
AddED(sccno[i], sccno[v]);
if (!toposort(n + scc_count))
cout<<"Shit!"<<endl;
sillys(n + scc_count);
return 0;
}
To be added