#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
int n;
int a[101][101];
int indegree[101];
int outdegree[101];
int bfs();
int ans[101][101];
int compare();
void print(int i);
int main()
{
int m,i,j,c,d;
scanf("%d%d",&n,&m);
for (i=1;i<=m;i++)
{
scanf("%d%d",&c,&d);
a[c][d]=1;
outdegree[c]++;
indegree[d]++;
}
if (!bfs())
printf("No\n");
else
if (compare())
{
printf("Yes\n");
printf("路径为:\n");
print(1);
}
else
printf("No\n");
return 0;
}
int bfs()
{
queue<int>q;
int i;
int vis[101],temp;
for (i=1;i<=n;i++)
vis[i]=0;
q.push(1);
vis[1]=1;
while(!q.empty())
{
temp=q.front();
q.pop();
for (i=1;i<=n;i++)
if ((a[temp][i])&&(!vis[i]))
{
vis[i]=1;
q.push(i);
}
}
for (i=1;i<=n;i++)
if (!vis[i])
{
printf("%d\n",i);
return 0;
}
return 1;
}
int compare()
{
int i;
for (i=1;i<=n;i++)
if (indegree[i]!=outdegree[i])
return 0;
return 1;
}
void print(int i)
{
int v;
for (v=1;v<=n;v++)
if ((a[i][v])&&(!ans[i][v]))
{
ans[i][v]=1;
printf("%d->%d\n",i,v);
print(v);
}
}
转载于:https://www.cnblogs.com/ukcxrtjr/p/11188066.html