# 迷宫城堡

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

Problem Description

Input

Output

Sample Input
3 3 1 2 2 3 3 1 3 3 1 2 2 3 3 2 0 0

Sample Output
Yes No

Author
Gardon

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<vector>
using namespace std;
const int maxn=100010;
int MIN(int a,int b){
return a<b?a:b;
}
int dfs_clock,scc_cnt;
int sccno[maxn];
bool instack[maxn];
int dfn[maxn];
int low[maxn];
vector<int>scc[maxn];
stack<int>S;
struct Node{
int to,next;
}A[maxn];
void init(){
memset(low,0,sizeof(low));
memset(instack,false,sizeof(instack));
memset(sccno,0,sizeof(sccno));
memset(dfn,0,sizeof(dfn));
dfs_clock=scc_cnt=0;
}
void tarjan(int u,int pre){
dfn[u]=low[u]=++dfs_clock;
S.push(u);int v;instack[u]=true;
v=A[k].to;
if(!dfn[v]){
tarjan(v,u);
low[u]=MIN(low[u],low[v]);
}
else if(instack[v]){
low[u]=MIN(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
scc_cnt++;scc[scc_cnt].clear();
while(1){
v=S.top();S.pop();
instack[v]=false;
sccno[v]=scc_cnt;
scc[scc_cnt].push_back(v);
if(u==v)break;
}
}
}
int main()
{
int n,m,i,j,k;
while(scanf("%d%d",&n,&m),n||m){
init();int a,b;
for(i=0;i<m;++i){
scanf("%d%d",&a,&b);
A[i].to=b;
}
for(i=1;i<=n;++i){
if(!dfn[i])tarjan(i,-1);
}
if(scc_cnt==1)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：hdoj1269迷宫城堡【scc】 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)