[tarjan缩点]

tarjan缩点

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lowbit(a) ((a) & -(a))
#define clean(a, b) memset(a, b, sizeof(a))
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 9;

int _;

//========================================================================
struct edge
{
    int cost,next,to;
}e[maxn];
int head[maxn],dfn[maxn],low[maxn],cnt=0,top=0,root,flag[maxn];
int n,m;
void init()
{
    clean(head,-1);
    top=0;
}
void insert_(int u,int v,int c)
{
    e[top].to=v;
    e[top].cost=c;
    e[top].next=head[u];
    head[u]=top++;
}
void tarjan(int now,int fa)
{
    int child=0,i,j;
    cnt++;
    dfn[now]=low[now]=cnt;
    for(int i=head[now];i!=-1;i=e[i].next)
    {
        if(dfn[e[i].to]==0) 
        {
            child++;
            tarjan(e[i].to,now);
            low[now]=min(low[now],low[e[i].to]);
            if(now!=root&&low[e[i].to]>=dfn[now]) flag[now]=1; 
            if(now==root&&child==2)  flag[now]=1;
        } 
        else if(e[i].to!=fa) 
        {
            low[now]=min(low[now],dfn[e[i].to]);
        }
    }
}
//========================================================================
int main()
{
    init();
    int u,v,w;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&u,&v);
        insert_(u,v,0);
        insert_(v,u,0);
        //
    } 
    root=1;
    tarjan(1,root);
    for(int i=1;i<=n;i++)
    {
        if(flag[i]==1) printf("%d ",i);
    }
    printf("\n");
    return 0;
}
posted @ 2020-07-17 17:08  L·S·D  阅读(122)  评论(0编辑  收藏  举报