爆零求调,不是tarjan

B3609 [图论与代数结构 701] 强连通分量

Starmoon_dhw @ 2024-05-26 14:49:26

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;
vector<int> G[maxn], G1[maxn];
stack<int> t; 
bool vis[maxn]; 
int sum;
int scc[maxn];
int n,m,u,v;
void dfs1(int u)
{
    vis[u]=1;
    for(int i=0;i<G[u].size();i++)
    {
        v=G[u][i];
        if(!vis[v]) dfs1(v);
    }
    t.push(u);
}
void dfs2(int u, int cnt)
{ 
    vis[u]=1;
    scc[u]=sum;
    for (int i=0;i<G1[u].size();i++)
    {
        int v=G1[u][i];
        if(!vis[v])
            dfs2(v,sum);
    }
}
int main()
{
    cin>>n>>m; 
    for (int i=1;i<=m;i++)
    {
        cin>>u>>v;
        G[u].push_back(v);
        G1[v].push_back(u);
    }
    for(int i=1;i<=n;i++)
        if(!vis[i]) 
            dfs1(i);
    memset(vis,0,sizeof(vis)); 
    while(!t.empty())
    { 
        u=t.top();
        t.pop();
        if(!vis[u])
        dfs2(u,++sum);
    }
    cout<<sum<<"\n";
    for(int i=1;i<=n;i++)
    {   
        for(int j=1;j<=n;j++)
        if(scc[j]==i)
            cout<<j<<" ";   
        cout<<"\n"; 
    }
    return 0;
}
/*for(int i=1;i<=n;i++)
        if (vis[color[i]]) {
            for(int j=0;j<col[color[i]].size();j++) cout<<col[color[i]][j]<<" ";
            cout<<endl;
            vis[color[i]]=false;
        }*/

by Starmoon_dhw @ 2024-05-26 14:49:58

感觉我输出有问题,看了题解改了一下还是错的


|