hdu1269——迷宫城堡——————【kosaraju】

概念解释    [强连通]——有向图G中,任意点对能相互到达,即存在路径,则称为强连通。

                              [连通分量]——指的是无向图中的极大连通子图。一个连通图的生成树是一个极小连通子图。


 

//只是判断是否强连通
#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
#include<vector>
using namespace std;
const int MAXV=11000;               
vector<int>adj[MAXV],an_adj[MAXV];  //用vector来存原图和转置图
bool visted[MAXV];                  //标记
int n,m;
bool dfs(int tmp,vector<int>* Graph)
{
    stack<int>st;
    int cnt=0;                      //计算有多少点是连着的
    visted[tmp]=1;
    st.push(tmp);
    while(!st.empty())
    {
        
        int u=st.top();
        cnt++;
        st.pop();
        for(int i=0; i<Graph[u].size(); i++){

            if(visted[Graph[u][i]])
                continue;
            visted[Graph[u][i]]=1;
            st.push(Graph[u][i]);
        }
    }
    if(cnt==n)
        return true;
    return false;
}
void Transform()                //将原图转置
{

    for(int i=1; i<=n; i++)
    {

        for(int j=0; j<adj[i].size(); j++)
        {

            an_adj[adj[i][j]].push_back(i);
        }
    }
}
bool kosaraju()
{

    memset(visted,0,sizeof(visted));
    if(!dfs(1,adj))         //here is 1
        return false;
    memset(visted,0,sizeof(visted));
    Transform();           
    if(dfs(1,an_adj))      //there must be 1
        return true;
    return false;
}
int main()
{
    int a,b;
    while(scanf("%d%d",&n,&m)!=EOF&&(n+m))
    {
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&a,&b);
            adj[a].push_back(b);        //入图
        }
        bool ans= kosaraju();
        ans==true? printf("Yes\n"):printf("No\n");
        for(int i=0;i<=n;i++){
            adj[i].clear();
            an_adj[i].clear();
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值