边双50pts求调

P8436 【模板】边双连通分量

Qerucy @ 2024-10-23 07:45:16

rt,窝手膜了几组重边和自环的样例都没寄,求调或hack

代码:

#include<bits/stdc++.h>

using namespace std;

int n,m;
int a[500010];
int x[500010],y[500010];
int tim,cnt;
int id[500010];
int dfn[500010];
int low[500010];
bool vis[500010];
bool in[500010];
int tot=1;
bool qwq[500010];
int ans;

struct node{
    int to,id;
};

vector<node>v[500010];
vector<int>ne[500010];
stack<int>s;

void tarjan(int x){
    dfn[x]=++tim;
    low[x]=dfn[x];
    s.push(x);
    in[x]=1;
    for(int i=0;i<v[x].size();i++){
        if(qwq[v[x][i].id]) continue;
        qwq[v[x][i].id]=qwq[v[x][i].id^1]=1;
        int to=v[x][i].to;
        if(dfn[to]){
            if(in[to]) low[x]=min(low[x],low[to]);
            continue;
        }
        tarjan(to);
        low[x]=min(low[x],low[to]);
    }
    if(dfn[x]==low[x]){
        id[x]=++cnt;
        ne[cnt].push_back(x);
        in[x]=0;
        while(!s.empty()){
            if(s.top()==x){
                s.pop();
                break;
            }
            int now=s.top();
            s.pop();
            id[now]=cnt;
            ne[cnt].push_back(now);
            in[now]=0;
        }
    }
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&x[i],&y[i]);
        v[x[i]].push_back((node){y[i],++tot});
        v[y[i]].push_back((node){x[i],++tot});
    }
    for(int i=1;i<=n;i++){
        if(!dfn[i]){
            tarjan(i);
        }
    }

    printf("%d\n",cnt);
    for(int i=1;i<=cnt;i++){
        printf("%d ",ne[i].size());
        for(auto x:ne[i]){
            printf("%d ",x);
        }
        puts("");
    }
    return 0;
}

by Qerucy @ 2024-10-23 08:28:10

空间开小了(


|