HDU 1269 迷宫城堡 [强连通分量] [Tarjan]

9 篇文章 0 订阅
4 篇文章 0 订阅

迷宫城堡
Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

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

Source
HDU 2006-4 Programming Contest


那么就是看是不是只有一个SCC就好了。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
const int maxn=100005;
const int INF=0x3f3f3f3f;
struct Edge
{
    int to,next;
}edge[maxn];
int head[maxn];
int maxedge;
inline void addedge(int u,int v)
{
    edge[++maxedge]=(Edge){ v,head[u] };
    head[u]=maxedge;
}
int n,m;
bool init()
{
    scanf("%d%d",&n,&m);
    if(!n && !m) return false;
    memset(head,-1,sizeof(head));
    maxedge=-1;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        addedge(x,y);
    }
    return true;
}
stack <int> s;
bool insta[maxn];
int dfs_clock;
int scc_cnt;
int dfn[maxn],low[maxn],sccno[maxn];
void dfs(int u)
{
    dfn[u]=low[u]=++dfs_clock;s.push(u);insta[u]=true;//remember to insta!
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v=edge[i].to;
        if(!dfn[v])
        {
            dfs(v);
            low[u]=min(low[u],low[v]);
        }
        else if(insta[v]) low[u]=min(low[u],dfn[v]);
    }
    if(low[u]>=dfn[u])
    {
        scc_cnt++;
        int tmp;
        do
        {
            tmp=s.top();s.pop();insta[tmp]=false;
            sccno[tmp]=scc_cnt;
        }while(tmp!=u);
    }
}
void tarjan()
{
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(sccno,0,sizeof(sccno));
    dfs_clock=0;scc_cnt=0;
    for(int i=1;i<=n;i++)
        if(!dfn[i]) dfs(i);
}
int main()
{
    #ifdef Local
    freopen("cattle.in","r",stdin);
    freopen("cattle.out","w",stdout);
    #endif
    while(init())
    {
        tarjan();
        if(scc_cnt^1) printf("No\n");
        else printf("Yes\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值