萌新刚学 tarjan 1ms 0pts 求条

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

imzfx_Square @ 2024-07-25 14:32:45

rt,条半天死活条不出来 QwQ

#include<bits/stdc++.h>
using namespace std;
const int N=11451,M=114514;
struct node{
    int to,next;
}e[M];
int h[N],cnt;
int n,m,u,v;
int dfn[N],low[N],tot,ans;
//dfn dfs序 low 子树能追溯最早的栈中节点 
int f[N];//属于哪个强联通分量 
int st[N],len;
bool vis[N],flag[N];
vector<int> ansv[N];
void Link(int u,int v){
    e[++cnt].to=v;
    e[cnt].next=h[u];
    h[u]=cnt;
}
void tarjan(int u){
    dfn[u]=low[u]=++tot;
    st[++len]=u;
    vis[u]=true;
    for(int i=h[u];i;i=e[i].next){
        v=e[i].to;
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }else if(vis[v]){
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u]){
        ans++;
        ansv[ans].push_back(u);
        while(st[len]!=u){
            ansv[ans].push_back(st[len]);
            vis[st[len]]=false;
            f[st[len]]=ans;
            len--;
        }
        vis[u]=false;
        f[u]=ans;
        len--;
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>u>>v;
        Link(u,v);
    }
    for(int i=1;i<=n;i++)
        if(!dfn[i])tarjan(i);
    for(int i=1;i<=ans;i++)
        sort(ansv[i].begin(),ansv[i].end());
    cout<<ans<<endl;
    for(int i=1;i<=n;i++){
        if(!flag[f[i]]){
            flag[f[i]]=true;
            for(int j=0;j<ansv[f[i]].size();j++)
                cout<<ansv[f[i]][j]<<' ';
            cout<<endl;
        }
    }
    return 0;
}

by kind_aunt @ 2024-07-25 21:44:03

qpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqp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