tarjan缩点模板 poj 2186

代码如下:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=50000+5;
int n,m,ans,hd[maxn],to[maxn],nxt[maxn],tot;
int mp[maxn],dfn[maxn],low[maxn],cnt,C;//Tarjan,mp[]&C缩点
int stk[maxn],tail=0;
bool instk[maxn]; 
int du[maxn];
inline int read(){
    char ch=getchar();
    int f=1,x=0;
    while(!(ch>='0'&&ch<='9')){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
        x=x*10+ch-'0',ch=getchar();
    return f*x;
}
inline void add(int x,int y){
    to[tot]=y;
    nxt[tot]=hd[x];
    hd[x]=tot++;
}
void tarjan(int root){
    dfn[root]=low[root]=++cnt;
    stk[++tail]=root;//root入栈
    instk[root]=1;
    for(int i=hd[root];i!=-1;i=nxt[i]){
        if(dfn[to[i]]==0)
            tarjan(to[i]),low[root]=min(low[root],low[to[i]]);
        else if(instk[to[i]])
            low[root]=min(low[root],dfn[to[i]]);
    } 
    if(low[root]==dfn[root]){
        C++;
        int tmp;
        do{
            tmp=stk[tail--];
            instk[tmp]=0;
            mp[tmp]=C;
        }while(tmp!=root);
    }
}
signed main(void){
    n=read(),m=read();
    memset(hd,-1,sizeof(hd)),tot=0;
    for(int i=1,x,y;i<=m;i++)
        x=read(),y=read(),add(x,y);
    for(int i=1;i<=n;i++)
        if(dfn[i]==0)
            tarjan(i);//如果未访问过,dfs
    for(int i=1;i<=n;i++)
        for(int j=hd[i];j!=-1;j=nxt[j])//遍历临接表 
             if(mp[i]!=mp[to[j]])
                du[mp[i]]++;
    int sum=0,tmp=0;
    for(int i=1;i<=C;i++)
        if(du[i]==0)
            sum++,tmp=i;
    if(sum==1)
        for(int i=1;i<=n;i++)
            ans+=mp[i]==tmp;
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值