HDU 1269

来源:互联网 发布:牙齿填骨粉的利弊 知乎 编辑:程序博客网 时间:2024/06/10 01:22

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

迷宫城堡

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


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 31 22 33 13 31 22 33 20 0
 

Sample Output
YesNo
 

Author
Gardon
 

Source
HDU 2006-4 Programming Contest
 
tarjan模板题,判断是否是强连通图。
/*************************************************************************    > File Name: HDU/1269.cpp    > Author: magicyang    > Mail:273868471@qq.com    > Created Time: 2014年08月31日 星期日 10时48分55秒 ************************************************************************/#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<queue>#include<vector>#include<stack>using namespace std;const int maxn=10005;vector<int>g[maxn];stack<int>s;int dfs_clk,scc_cnt;int pre[maxn],lowlink[maxn],scc[maxn];void dfs(int u){    pre[u]=lowlink[u]=++dfs_clk;    s.push(u);    for(int i=0;i<g[u].size();i++)    {        int v=g[u][i];        if(!pre[v])        {            dfs(v);            lowlink[u]=min(lowlink[u],lowlink[v]);        }        else if(!scc[v])        {            lowlink[u]=min(lowlink[u],pre[v]);        }    }    if(lowlink[u]==pre[u])    {        scc_cnt++;        for(;;)        {            int x=s.top();s.pop();            scc[x]=scc_cnt;            if(x==u) break;        }    }}void find_scc(int n){    dfs_clk=scc_cnt=0;    memset(scc,0,sizeof scc);    memset(pre,0,sizeof pre);    for(int i=1;i<=n;i++)        if(!pre[i]) dfs(i);}int main(){    int n,m;    while(cin>>n>>m)    {        if(n==0&&m==0) break;        for(int i=1;i<=10000;i++)            g[i].clear();        for(int i=1;i<=m;i++)        {            int x,y;            scanf("%d%d",&x,&y);            g[x].push_back(y);        }        find_scc(n);        if(scc_cnt>1) cout<<"No"<<endl;        else cout<<"Yes"<<endl;    }}


0 0
原创粉丝点击