码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1269:强连通分量

时间:2016-02-05 22:20:52      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

判断是不是只有一个强连通分量就好了。。。

------------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define adde(u,v) add(u,v),add(v,u)
const int nmax=10005;
int read(){
    int x=0,f=1;char c=getchar();
    while(!isdigit(c)){
        if(c==-) f=-1;
        c=getchar();
    }
    while(isdigit(c)){
        x=x*10+c-0;
        c=getchar();
    }
    return  x*f;
} 
struct edge{
    int from,to;
    edge *next;
    /*edge(int _from,int _to):
        from(_from),to(_to){};*/
};
edge e[100005],*pt,*head[nmax];
int sccno[nmax],pre[nmax],low[nmax],scc_cnt,dfn_clock,n,m;
void add(int u,int v){
    pt->to=v;pt->next=head[u];head[u]=pt++;
}
stack<int>S;
void dfs(int x){
    low[x]=pre[x]=++dfn_clock;
    S.push(x);
    for(edge *ee=head[x];ee;ee=ee->next){
        int to=ee->to;
        if(!pre[to]){
            dfs(to);
            low[x]=min(low[x],low[to]);
        }else if(!sccno[to]){
            low[x]=min(low[x],pre[to]);
        }
    }
    if(pre[x]==low[x]){
        scc_cnt++;
        while(1){
            int u=S.top();
            S.pop();
            sccno[u]=scc_cnt;
            if(u==x) break;
        }
    }
    return ;
}
void init(){
    clr(pre,0);
    dfn_clock=scc_cnt=0;
    clr(head,0);
    clr(sccno,0);
    pt=e;
}
int main(){
    while(scanf("%d%d",&n,&m)==2&&n+m){
        init();
        rep(i,m){
            int s=read(),t=read();
            add(s,t);
        }
        rep(i,n){
            if(!pre[i]) dfs(i);
         }
         if(scc_cnt==1) printf("Yes\n");
         else printf("No\n");
    }
    return 0;
}

------------------------------------------------------------------------------------------

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

 Status

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

 Status

hdu1269:强连通分量

标签:

原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5183607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!