HDOJ 1269 迷宫城堡

本文介绍了一种用于判断有向图中是否存在任意点到各点都为连通的算法,通过强连通分量和tarjan算法实现。代码示例及运行结果展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:判断有向图中是否存在任意点n到各点都为连通

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1269

思路:强连通分量,tarjan,判断强连通分量的数量,直接套模板了。

以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
118223082014-10-08 09:19:41Accepted126978MS1848K1169 BG++luminous11

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>

using namespace std;

#define MAX_V 11111

int V, E;
int dfn[MAX_V], low[MAX_V];
bool instack[MAX_V];
int din;
int cnt;
stack<int> s;
vector<int> Edge[MAX_V];

void tarjan ( int x )
{
    instack[x] = true;
    dfn[x] = low[x] = din++;
    s.push ( x );
    for ( int i = 0; i < Edge[x].size(); i ++ )
    {
        int j = Edge[x][i];
        if ( ! dfn[j] )
        {
            tarjan ( j );
            low[x] = min ( low[x], low[j] );
        }
        else if (instack[j])
            low[x] = min ( low[x], dfn[j] );
    }
    if ( dfn[x] == low[x] )
    {
        cnt++;
        int tmp;
        do
        {
            tmp = s.top();
            s.pop();
            instack[tmp] = false;
        }
        while ( x != tmp );
    }
}

int main()
{
    while ( scanf ( "%d%d" , &V, &E ) && ( V || E ) )
    {
        for ( int i = 0; i < V; i ++ )
            Edge[i].clear();
        for ( int i = 0; i < E; i ++ )
        {
            int u, v;
            scanf ( "%d%d", &u, &v);
            Edge[u - 1].push_back(v - 1);
        }
        memset ( dfn, 0, sizeof ( dfn ) );
        memset ( instack, false, sizeof ( instack ) );
        din = 0;
        cnt = 0;
        for ( int i = 0; i < V; i ++ )
        {
            if ( ! dfn[i] )
                tarjan ( i );
        }
        if ( cnt == 1 )
            printf ( "Yes\n" );
        else
            printf ( "No\n" );
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值