The point double connected component of undirected graph

Source: Internet
Author: User

Point-to-double connected component: There are at least two points in the graph where there are no duplicate paths, then the graph is double connected; the maximal point of undirected graph-the double connected sub-graph is called the double connected component;

The process of solving the double-connected component of a point is actually the process of finding a cutting point:

Build a stack, store the current two connected branches, in the process of searching, each find a tree edge, the edge is added to the stack. If you encounter a side of the branch edge satisfies Pre[u] <= low[v], it means that U is a cut point. At this point, the edge is removed from the top of the stack until it encounters a (u,v) edge, and the extracted edges and their connected vertices form a point-double-connected component.

Note: The cut point can belong to multiple points of two connected components, the remaining points can only belong to a single point of two connected components, each edge is exactly a point of two connected components;

Point two-connected component algorithm:

#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <climits > #include <cctype> #include <iostream> #include <algorithm> #include <queue> #include < vector> #include <map> #include <set> #include <string> #include <stack> #define LL Long Long # Define MAX 1010 #define MAXN 5000010 #define INF int_max #define EPS 1e-6 #define REP (i,n) for (INT i=0; i< (n); i++) #d

Efine for (i,s,t) for (int i= (s); i<= (t); i++) using namespace std;

struct edge{int u,v;}; int pre[max],bccno[max],dfs_clock,bcc_cnt,iscut[max],n;  BCCNO record node belongs to which point-double connected branch vector<int>g[max],bcc[maxn];
BCC records which points are included in the two connected branches of each point stack<edge>s;  vector<edge>edges[max];
		Record each connected component containing those side void Init () {for (int i=0; i<=n; i++) {g[i].clear ();
		Bcc[i].clear ();
	Edges[i].clear (); } while (!
	S.empty ()) S.pop ();
	memset (pre,0,sizeof (pre));
	memset (iscut,0,sizeof (iscut));
	memset (bccno,0,sizeof (BCCNO)); Dfs_cLock = bcc_cnt = 0;
	} int dfs (int u,int fa) {int Lowu = pre[u] = ++dfs_clock;
	int child = 0;
		for (int i=0; i<g[u].size (); i++) {int v = g[u][i];
		Edge e = (edge) {u,v};
			if (!pre[v]) {S.push (e);
			child++;
			int LOWV = DFS (v,u);
			Lowu = min (Lowv,lowu);
				if (LOWV >= pre[u]) {Iscut[u] = true;
				bcc_cnt++;
				Bcc[bcc_cnt].clear ();
					while (1) {Edge x = S.top ();
					Edges[bcc_cnt].push_back (x);
					S.pop ();
						if (bccno[x.u]! = bcc_cnt) {bccno[x.u] = bcc_cnt;
					Bcc[bcc_cnt].push_back (X.U);
						} if (BCCNO[X.V]! = bcc_cnt) {BCCNO[X.V] = bcc_cnt;
					Bcc[bcc_cnt].push_back (X.V);
				} if (x.u = = U && x.v = = v) break;
			}}}} else if (Pre[v] < Pre[u] && v! = FA) {s.push (e);
		Lowu = min (lowu,pre[v]);
	}} if (fa<0 && child = = 1) iscut[u] = 0;
return LOWU;
	} void Find_bcc () {for (int i = 1; i<=n; i++) {if (!pre[i]) DFS (I,-1);
	}} int main () {int m; while (scanf ("%d%D ", &n,&m)! = EOF) {init ();
		int x, y;
			for (int i=0; i<m; i++) {scanf ("%d%d", &x,&y);
			G[x].push_back (y);
		G[y].push_back (x);
		} find_bcc ();
			for (int i=1; i<=bcc_cnt; i++) {for (int j = 0; J<bcc[i].size (); j + +) printf ("%d", bcc[i][j]);
			printf ("\ n");
		for (int j=0; j<edges[i].size (); j + +) printf ("(%d,%d) \ n", EDGES[I][J].U,EDGES[I][J].V);
}} return 0;

 }




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.