分析:BFS判断是否有环。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int gra[200][200];
int vis[100];
void bfs(int n)
{
memset(vis,0,sizeof(vis));
vis[1] = 1;
int q[100];
int in = 0,out = 0, f = 0;
q[in ++] = 1;
while(in > out)
{
int x = q[out ++];
for(int i = 1; i <= n; i ++)
{
if(gra[x][i])
{
if(vis[i]){
f = 1;
break;
}
else {
vis[i] = 1;
q[in ++] = i;
}
}
}
if(f)break;
}
if(f)printf("NO
");
else printf("YES
");
}
int main()
{
int n,m,u,v;
while(scanf("%d%d",&n,&m)!=EOF){
memset(gra,0,sizeof(gra));
for(int i = 0; i < m; i ++){
scanf("%d%d",&u,&v);
gra[u][v] = 1;
}
bfs(n);
}
return 0;
}