50分求助

P8436 【模板】边双连通分量

wo_hen_la @ 2024-07-07 11:42:36

WA了subtask#1#3错了几个点

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

#define pb push_back
#define int long long
#define P 998244353
#define HP 1000000000000002097
#define HP2 1000000000000000003
#define N (int)1e6+5

int read(){ char ch=getchar();int x=0,f=1;while(ch>'9' || ch<'0'){if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();return x*f; }
void print(int x){ if(x<0) putchar('-'),x=-x;if(x>9)print(x/10);putchar(x%10+'0'); }

int n,m,ti;
int low[N],dfn[N];
bool vis[N];
map<pair<int,int> ,bool> b; 
vector<int> e[N],ans[N];
void tarjan(int u,int fa)
{
    dfn[u]=low[u]=++ti;
    for(auto v:e[u]){
        if(v==fa) continue;
        if(!dfn[v]){
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(low[v]>dfn[u]) b[{u,v}]=b[{v,u}]=1;
        }
        else low[u]=min(low[u],dfn[v]);
    }
}
void dfs(int u,int cnt)
{
    ans[cnt].pb(u);
    for(auto v:e[u]){
        if(vis[v] || b[{u,v}]) continue;
        if(!vis[v]) vis[v]=1;
        dfs(v,cnt);
    } 
} 
main()
{
    ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1,u,v;i<=m;i++){
        cin>>u>>v;  
        if(u!=v) e[u].pb(v);e[v].pb(u);
    }
    for(int i=1;i<=n;i++){
        if(!dfn[i]) tarjan(i,0);
    }
    int cnt=0;
    for(int i=1;i<=n;i++){
        if(!vis[i]) vis[i]=1,dfs(i,++cnt);
    }
    cout<<cnt<<"\n";
    for(int i=1;i<=cnt;i++){
        cout<<ans[i].size()<<" ";
        for(auto v:ans[i]) cout<<v<<" ";
        cout<<"\n";
    }
    return 0;
}

by hao_zi6366 @ 2024-08-01 20:44:37

考虑到重边不可能是桥的情况


|