AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

Tarjan有向图的强连通分量模板

作者: 作者的头像   风城玫瑰.01 ,  2023-11-28 22:10:12 ,  所有人可见 ,  阅读 89


0


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

const int N = 1e4 + 10 , M = 1e5 + 10;//N为点数,M为边数

int n,m;//n个点,m条边
int h[N],e[M],ne[M],idx;//存图
int dfn[N],low[N],ts;//时间戳dfn[x]表示x点第一次被访问的顺序,追溯值low[x]表示从x点出发,能够访问到的最早时间戳
int stk[N],in_stk[N],top;//stk[]数组模拟栈,in_stk[]标志某点是否在栈中
int scc[N],sizes[N],cnt;//

void add(int a,int b)
{
    e[idx] = b , ne[idx] = h[a] , h[a] = idx ++ ;
}

void Tarjan(int x)
{
    dfn[x] = low[x] = ++ ts;
    stk[++ top] = x , in_stk[x] = 1;

    for( int i = h[x] ; ~i ; i = ne[i] )
    {
        int j = e[i];
        if(!dfn[j])
        {
            Tarjan(j);
            low[x] = min(low[x] , low[j]);
        }
        else if(in_stk[j]) low[x] = min(low[x] , dfn[j]);
    }

    if(low[x] == dfn[x])
    {
        int y; ++ cnt;
        do{
            y = stk[top --] , in_stk[y] = 0;
            scc[y] = cnt;
            ++ sizes[cnt];
        }while(y != x);
    }
}

0 评论

App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息