P3388 【模板】割点(割顶)

#include <bits/stdc++.h>
#define inf 2333333333333333
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(int i=a;i<=b;++i)
//by war
//2020.8.4
using namespace std;

int n,m,x,y,cnt,ans;
int dfn[N],low[N],del[N];
bool vis[N];
struct node{
    int n;
    node *next;
}*e[N];

void in(int &x){
    int y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(int x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

void push(int x,int y){
    node *p;
    p=new node();
    p->n=y;
    if(e[x]==0)
        e[x]=p;
    else{
        p->next=e[x]->next;
        e[x]->next=p;
    }
}

void tarjan(int x,int fa){
    dfn[x]=low[x]=++cnt;
    vis[x]=1;
    int num=0;
    for(node *i=e[x];i;i=i->next){
        if(!dfn[i->n]){
            num++;
            tarjan(i->n,x);
            low[x]=min(low[x],low[i->n]);
            if(low[i->n]>=dfn[x] && x!=fa && !del[x]) {
                ans++;
                del[x]=1;
            }
        }
        else
            if(vis[i->n])
                low[x]=min(low[x],dfn[i->n]);
    }
    if(x==fa && num>1 && !del[x]){
        ans++;
        del[x]=1;
    }
}

int main(){
    in(n);in(m);
    For(i,1,m){
        in(x);in(y);
        push(x,y);
        push(y,x);
    }
    For(i,1,n) if(!dfn[i]) tarjan(i,i);
    o(ans);p('\n');
    For(i,1,n)
        if(del[i]) o(i),p(' ');
    return 0;
}

 

posted @ 2020-08-04 23:25  WeiAR  阅读(131)  评论(0编辑  收藏  举报