zoukankan      html  css  js  c++  java
  • HDU_1269_tarjan求强连通分量

    迷宫城堡

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 12801    Accepted Submission(s): 5710


    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
     
     
    Tarjan算法是基于对图深度优先搜索的算法,每个强连通分量为搜索树中的一棵子树。搜索时,把当前搜索树中未处理的节点加入一个堆栈,回溯时可以判断栈顶到栈中的节点是否为一个强连通分量。
    定义DFN(u)为节点u搜索的次序编号(时间戳),Low(u)为u或u的子树能够追溯到的最早的栈中节点的次序号。
    当DFN(u)=Low(u)时,以u为根的搜索子树上所有节点是一个强连通分量
     
    重点理解dfn数组和low数组的作用,算法结合代码不难理解。
     
    此题即是问给定的图的强连通分量的个数是否为1.
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<algorithm>
    #include<stdlib.h>
    #include<stack>
    #include<vector>
    using namespace std;
    
    #define N 10005
    
    stack<int>sta;
    vector<int>gra[N];
    int dfn[N],low[N],now,vis[N],sum;
    int n,m;
    
    void ini()
    {
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(vis,0,sizeof(vis));
        now=sum=0;
        for(int i=1;i<=n;i++)
            gra[i].clear();
        while(sta.empty()==0)
            sta.pop();
    }
    
    void tarjan(int s)
    {
        vis[s]=2;
        dfn[s]=low[s]=++now;
        sta.push(s);
        for(int i=0;i<gra[s].size();i++)
        {
            int t=gra[s][i];
            if(dfn[t]==0)
            {
                tarjan(t);
                low[s]=min(low[s],low[t]);
            }
            else
                if(vis[t]==2)
                    low[s]=min(low[s],dfn[t]);
        }
        if(low[s]==dfn[s])
        {
            sum++;
            while(!sta.empty())
            {
                int t=sta.top();
                sta.pop();
                vis[t]=1;
                if(t==s) break;
            }
        }
    }
    
    void read()
    {
        for(int i=1;i<=m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            gra[a].push_back(b);
        }
    }
    
    void run()
    {
        for(int i=1;i<=n;i++)
            if(!dfn[i])
                tarjan(i);
        if(sum>1)
            puts("No");
        else
            puts("Yes");
    }
    
    int main()
    {
        while(scanf("%d%d",&n,&m),n+m)
        {
            ini();
            read();
            run();
        }
    }
  • 相关阅读:
    [LeetCode] Word Ladder II
    [LeetCode] Edit Distance
    [LeetCode] Merge Intervals
    内存分配与Segmentation fault
    结构体 指针 数组
    resolv.conf
    无法启动xwindow
    stopping NetworkManager daemon failed
    linux 挂载光盘:mount: you must specify the filesystem type
    修改主机hostname
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/5727702.html
Copyright © 2011-2022 走看看