Templates - cut point

Finally cut point template to tune out

Cut definition of points:

Undirected connected graphs, in which if a connection point and this point all edges removed, FIG no longer in communication, then this point is called the cut points (cut vertex / articulation point).

For example, in the figure, the points 0,3 are cut, because the 0 and 3 after removing any, is no longer communicating FIG. If 0 is removed, and the map is divided into two connected components 1, 3, 4; 3, if removed, then FIG 4 is divided into two connected components and 0,1,2.

 

Since it is tarjan seek, necessarily involves dfn [] and low [] two kinds of arrays, the definition can refer to other blog about the tarjan

And if the nature of a point cut point is, it has to meet

1. If he is the father node, the tree of his son more than two, then he must cut point

2. If he was an ordinary node, if his offspring low [] than his timestamp dfn [] or less, then it shows that he must go through his descendants to be able to go up, if it does not remove the entire map Unicom.

code show as below:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+7;
struct node{
    int nxt;
    int to;
}edge[2*maxn];
int head[maxn],cnt,tot;
bool check[maxn];
int cut[maxn];
void add(int x,int y){
    edge[++cnt].nxt=head[x];
    edge[cnt].to=y;
    head[x]=cnt;
}
int n,m,x,y;
int dfn[maxn],low[maxn],sta[maxn],Time;
void dfs(int x,int fa){
    dfn[x]=low[x]=++Time;
    int son=0;//统计以x为根的子树个数 
    for(int i=head[x];i;i=edge[i].nxt){
        int v=edge[i].to;
        if(!dfn[v]){
            dfs(v,fa);
            low[x]=min(low[x],low[v]);
            if(x==fa) son++;
            if(x!=fa&&low[v]>=dfn[x]) check[x]=true;//If it is not the root, but the child can go back to the last time is larger than that, indicating that the child must go through it, so he removed the original will not be China Unicom, the cut point is 
          }
           the else Low [the X-] = min (low [the X-], DFN [v]); // this is very important, it must be time to go and he could do was point of comparison, and not their relatively low, this may not search cutpoint 
    }
     IF ( X == FA && son> = 2 ) Check [FA] = to true ; // If the node is the root node and sub-tree is greater than 2, is cut point 
}
 int main () 
{ 
    Scanf ( " % D% D " , & n-, & m );
     for ( int I = . 1 ; I <= m; I ++ ) { 
        Scanf ( " % D% D " , & X, & Y); 
        the Add (X, Y); 
        the Add (Y, X);
    }
    for(int i=1;i<=n;i++){
        if(!dfn[i]) dfs(i,i);
    } 
    for(int i=1;i<=n;i++){
        if(check[i]) cut[++tot]=i; 
    }
    printf("%d\n",tot);
    sort(cut+1,cut+1+tot);
    for(int i=1;i<=tot;i++) printf("%d ",cut[i]);
    return 0; 
} 

About the code there is a small problem, is about

There dalao gives a good explanation on the Los Valley

 

Reproduced in: https: //www.cnblogs.com/LJB666/p/11000100.html

Guess you like

Origin blog.csdn.net/weixin_34007291/article/details/93223474