[Graph theory] tarjan cut point: template question: Luo Gu 3388

Split point

If there is a set of vertices, the vertex set and delete the edge in this set of all vertices associated after connected component increases in a non-directed graph, this is said set point is a set of cut points.

Seeking point cut

Tarjan by the process algorithm, we can see that, if the point u is a cut point, it must be descendants dfs sequence than small dots v, so that low [v] <low [u], u is removed at this point after removing the bound so that the strongly connected components in the ring, the subgraph is composed of strongly connected components not cut off.

Template question: Luo Gu 3388

Seeking the number of cut points and the number of

#include<bits/stdc++.h>
using namespace std;

const int maxn=20010;
int low[maxn],dfn[maxn],iscut[maxn];
int n,m,ans;
vector<int> g[maxn];
int st[maxn],top;
int deep;

void tarjan(int u,int fa)
{
    int child=0;
    int sz=g[u].size();
    dfn[u]=low[u]=++deep;
    for(int i=0;i<sz;i++)
    {
        int v=g[u][i];
        if(!dfn[v])
        {
            child++;
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(low[v]>dfn[u]) iscut[u]=1;
        }
        else
        {
            if(v!=fa&&dfn[v]<dfn[u]) low[u]=min(low[u],dfn[v]);
        }
    }
    if(fa<0&&child==1) iscut[u]=0;
}


int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for(int i=1;i<=n;i++)if(!dfn[i])
        tarjan(i,-1);
    for(int i=1;i<=n;i++) ans+=iscut[i];
    printf("%d\n",ans);
    for(int i=1;i<=n;i++) if(iscut[i]) printf("%d ",i);
    puts("");
    return 0;
}

Guess you like

Origin www.cnblogs.com/JWizard/p/11753930.html