Tarjan algorithm for solving the cut-point template of undirected connected graphs

Source: Internet
Author: User

#include <cstdio>#include<cstring>#include<cmath>#include<vector>#include<algorithm>using namespacestd;Const intmaxn=1111;//How many nodes are there?vector<int>G[MAXN];intVISITED[MAXN];//flag If the node has visitedintNode,edge;//number of verticesintTMPDFN;//The current depth-first search ordinal is logged in the DFS processintDFN[MAXN];//record Depth-first search ordinal for each vertexintLOW[MAXN];//The low value of each vertex, based on the value to determine if it is a joint pointintSon//How many children are at the root node, and if greater than or equal to 2, the root node is the joint pointintSUBNETS[MAXN];//record the number of connected components for each node (after the node is removed)voidinit () { for(intI=0; i<maxn;i++) g[i].clear (); low[1]=dfn[1]=1; TMPDFN=1; son=0; Memset (visited,0,sizeof(visited)); visited[1]=1; memset (Subnets,0,sizeof(subnets));}voidDfsintu) {     for(intI=0; I<g[u].size (); i++)    {        intv=G[u][i]; if(!Visited[v]) {Visited[v]=1; TMPDFN++; dfn[v]=low[v]=TMPDFN;            DFS (v); Low[u]=min (low[u],low[v]); if(low[v]>=Dfn[u]) {                if(u!=1) subnets[u]++; if(u==1) son++; }        }        Elselow[u]=min (low[u],dfn[v]); }}intMain () {scanf ("%d%d", &node,&edge);//Enter the number of nodes and the number of edgesInit ();//Initialize     for(intI=1; i<=edge;i++)    {        intu,v; scanf ("%d%d",&u,&v); //non-Tujian sideG[u].push_back (v);    G[v].push_back (U); }    //Solving cut pointsDfs1); //Compute root Node    if(son>1) subnets[1]=son-1;  for(intI=1; i<=node;i++)        if(Subnets[i]) printf ("node%d is a cut point, and after deletion there are%d connected components \ n", i,subnets[i]+1); return 0;}

Tarjan algorithm for solving the cut-point template of undirected connected graphs

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.