zoukankan      html  css  js  c++  java
  • 迷宫城堡 强连通

    Problem Description
    为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明可以通过这个通道由A房间到达B房间,但并不说明通过它可以由B房间到达A房间。Gardon需要请你写个程序确认一下是否任意两个房间都是相互连通的,即:对于任意的i和j,至少存在一条路径可以从房间i到房间j,也存在一条路径可以从房间j到房间i。
     
    Input
    输入包含多组数据,输入的第一行有两个数:N和M,接下来的M行每行有两个数a和b,表示了一条通道可以从A房间来到B房间。文件最后以两个0结束。
     
    Output
    对于输入的每组数据,如果任意两个房间都是相互连接的,输出"Yes",否则输出"No"。
     
    Sample Input
    3 3 1 2 2 3 3 1 3 3 1 2 2 3 3 2 0 0
     
    Sample Output
    Yes No
     
    判断强连通分量是否为1即可
    #include<bits/stdc++.h>
    using namespace std;
    //input by bxd
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i>=(b);--i)
    #define RI(n) scanf("%d",&(n))
    #define RII(n,m) scanf("%d%d",&n,&m)
    #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
    #define RS(s) scanf("%s",s);
    #define ll long long
    #define pb push_back
    #define REP(i,N)  for(int i=0;i<(N);i++)
    #define CLR(A,v)  memset(A,v,sizeof A)
    //////////////////////////////////
    #define inf 0x3f3f3f3f
    const int N=100000+5;
    int head[N];
    int pos;
    struct Edge
    {
        int to,nex;
    }edge[N];
    void add(int a,int b)
    {
        edge[++pos].nex=head[a];
        head[a]=pos;
        edge[pos].to=b;
    }
    int dfn[N],low[N],Stack[N],vis[N];
    int tot=0,index=0;
    int cnt=0;
    void tarjan(int x)
    {
        dfn[x]=low[x]=++tot;
        Stack[++index]=x;
        vis[x]=1;
        for(int i=head[x];i;i=edge[i].nex)
        {
            if(!dfn[edge[i].to])
            {
                tarjan(edge[i].to);
                low[x]=min(low[x],low[edge[i].to]);
            }
            else if(vis[edge[i].to])
                low[x]=min(low[x],dfn[edge[i].to]);
        }
        if(low[x]==dfn[x])
        {
            cnt++;
            do
            {
                vis[ Stack[index] ]=0;
                index--;
            }
            while(x!=Stack[index+1]);
        }
    }
    int main()
    {
        int n,m;
        while(~RII(n,m),n||m)
        {
            CLR(head,0);
            CLR(dfn,0);
            CLR(low,0);
            pos=tot=index=0;
            rep(i,1,m)
            {
                int a,b;RII(a,b);
                add(a,b);
            }
            cnt=0;
            rep(i,1,n)
                if(!dfn[i])tarjan(i);
            
            printf("%s
    ",cnt==1?"Yes":"No");
        }
        return 0;
    }
    View Code
     
     
  • 相关阅读:
    Gitlab的安装
    转 Java操作PDF之iText详细入门
    ElasticSearch聚合(转)
    谷歌搜索技巧(转)https://www.runningcheese.com/google
    自学elastic search
    WinForm richtextbox 关键字变红色
    https采集12306(复制)
    LTS
    学习Android MediaPlayer
    UML基础知识
  • 原文地址:https://www.cnblogs.com/bxd123/p/10795754.html
Copyright © 2011-2022 走看看