zoukankan      html  css  js  c++  java
  • 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
    ");
            else printf("No
    ");
        }
        return 0;
    }
  • 相关阅读:
    通用Logging框架设计
    slf4j 与各个 logging框架的适配器说明
    优雅的使用Spring
    Python : 反射
    Python: Tools
    Python : Class
    Python : Module
    Python 入门:基本语法
    docker:版本变更
    Linux: yum配置说明
  • 原文地址:https://www.cnblogs.com/wyh447154317/p/12241572.html
Copyright © 2011-2022 走看看