hdu1269 迷宫城堡

跑一遍tarjan,判断一下是否是强连通图

#include<bits/stdc++.h>
using namespace std;
const int M=1e5+5;
const int N=1e4+5;
struct E{
    int to,next;
}e[M];
int head[N],dfn[N],low[N],belong[N],tot,cnt,bcnt;
stack<int>s;
void add(int u,int v){
    e[++tot].to=v;
    e[tot].next=head[u];
    head[u]=tot;
}
void tarjan(int u){
    int v;
    dfn[u]=low[u]=++cnt;
    s.push(u);
    for(int i=head[u];i;i=e[i].next){
        v=e[i].to;
        if(!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]);
        else if(!belong[v]) low[u]=min(low[u],dfn[v]);//belong[i]
    }
    if(dfn[u]==low[u]){
        ++bcnt;
        do{ v=s.top();
            s.pop();
            belong[v]=bcnt;
        }while(u!=v);//不能在内部定义v 
    }
}
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)){
        if(n==0&&m==0)break;// 
        memset(e,0,sizeof(e));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(head,0,sizeof(head));//
        memset(belong,0,sizeof(belong));
        bcnt=cnt=tot=0;
        for(int i=1;i<=m;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
        }
        for(int i=1;i<=n;i++)
         if(!dfn[i])tarjan(i);
        if(bcnt==1)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

 

posted @ 2020-01-29 21:42  谨川  阅读(107)  评论(0编辑  收藏  举报