tarjan模板

今天下午就只看了一下tarjan算法。。。。妈蛋。。。太颓废了。。。这样不好不好~~

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
int root[10000],ans,cnt,scc,top;
int dfn[10000],low[10000];
int s[100000],belong[10000];
bool vis[10000];
struct node
{
    int e,next;
}a[10000];
void add(int s,int e)
{
    a[++ans].e=e;
    a[ans].next=root[s];
    root[s]=ans;
}
void tarjan(int u)
{
    dfn[u]=low[u]=++cnt;
    vis[u]=1;
    s[top++]=u;
    int v;
    for(int i=root[u];i!=-1;i=a[i].next)
    {
        v=a[i].e;
        if(dfn[v]==0)
        {
            tarjan(v);
            low[u]=low[u]>low[v]?low[v]:low[u];
        }
        else if(vis[v] && low[u]>dfn[v])
        low[u]=dfn[v];
    }
    if(dfn[u]==low[u])
    {
        ++scc;
        do
        {
            v=s[--top];
            vis[v]=0;
            belong[v]=scc;
        }while(u!=v);
    }
}
int main()
{
    int k=8;
    ans=cnt=scc=top=0;
   // top=0,cnt=0;
   ans=-1;
    memset(root,-1,sizeof(root));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(belong,-1,sizeof(belong));
    while(k--)
    {
        int s,e;
        scanf("%d%d",&s,&e);
        add(s,e);
    }
    tarjan(1);
    for(int i=1;i<=6;i++)
    {
        printf("%d  %d\n",i,belong[i]);
    }
    system("pause");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值