#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN=10010;
const int MAXM=300010;
const int INF=0x3f3f3f3f;
int N,M;
int scc;
int head[MAXN],tot;
int Index,top;
int low[MAXN],dfn[MAXN],Stack[MAXN],Belong[MAXN];
bool Instack[MAXN];
int num[MAXN];
struct Edge{
int u,v,nxt;
int w;
}edge[MAXM];
void init(){
tot=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w){
edge[tot].u=u;
edge[tot].v=v;
edge[tot].w=w;
edge[tot].nxt=head[u];
head[u]=tot++;
}
/*
void tarjian(int u){
int v;
low[u]=dfn[u]=++Index;
Stack[top++]=u;
Instack[u]=true;
for (int i=head[u];i!=-1;i=edge[i].nxt)
{
int v=edge[i].v;
if (!dfn[v]){
tarjian(v);
if (low[u]>low[v]) low[u]=low[v];
}
else if (Instack[v]&&low[u]>dfn[v])
low[u]=dfn[v];
}
if (low[u]==dfn[u])
{
scc++;
do{
v=Stack[--top];
Instack[v]=false;
Belong[v]=scc;
num[scc]++;
}while (v!=u);
}
}*/
void tarjian(int u)
{
int v;
low[u] = dfn[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for (int i = head[u] ; i != -1 ; i = edge[i].nxt)
{
int v = edge[i].v;
if (!dfn[v])
{
tarjian(v);
if (low[u] > low[v]) low[u] = low[v];
}
else if (Instack[v] && low[u] > dfn[v])
low[u] = dfn[v];
}
if (low[u] == dfn[u])
{
scc++;
do
{
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc;
num[scc]++;
}while (v != u);
}
}
void deal(){
memset(dfn,0,sizeof(dfn));
memset(Instack,false,sizeof(Instack));
Index=top=0;
scc=0;
memset(num,0,sizeof(num));
for (int i=1;i<=N;i++)
{
if (!dfn[i]) tarjian(i);
}
}
int main(){
while (scanf("%d%d",&N,&M)!=EOF)
{
if (N==0&&M==0) break;
init();
for (int i=0;i<M;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v,9797);
}
deal();
printf("%s\n",scc==1?"Yes":"No");
}
return 0;
}
迷宫城堡
最新推荐文章于 2022-08-03 14:07:24 发布