#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int instack[10008],low[10008],dfn[10008];
bool stack[10008],DFN[10008];
int n,m,top,indx,bet;
vector <int> vc[10008];
void Init()
{
bet = top = indx = 0;
memset(stack,false,sizeof(stack));
memset(low,0,sizeof(low));
memset(DFN,false ,sizeof(DFN));
memset(dfn,0,sizeof(dfn));
memset(vc,0,sizeof(vc));
}
void tarjan(int x)
{
int y;
low[x] = dfn[x] = ++indx;
instack[++top] = x;
DFN[x] = stack[x] = true;
for(int i=0;i<vc[x].size();i++)
{
y = vc[x][i];
if(!DFN[y])
{
tarjan(y);
low[x] = min(low[x],low[y]);
}
else
if(stack[y])
{
low[x] = min(low[x],dfn[y]);
}
}
if(dfn[x]==low[x])
{
++bet;
do
{
y = instack[top--];
stack[y] = false;
} while(x!=y);
}
}
int main()
{
while(~scanf("%d%d",&n,&m),n+m)
{
int a,b;
Init();
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
vc[a].push_back(b);
}
for(int i=1;i<=n;i++)
if(!DFN[i])
tarjan(i);
if(bet==1)puts("Yes");
else puts("No");
}
}
HDU 1269 强联通
最新推荐文章于 2022-11-25 19:10:15 发布