#include<iostream> // POJ 2553 #include<algorithm> #include<vector> using namespace std; const int Size=5005; int f[Size],HashF[10010],color[Size]; int p[Size],root; int n,m,time; bool Markadj[Size]; vector<vector<int>> adj(Size,vector<int>(0)); vector<vector<int>> adjT(Size,vector<int>(0)); bool cmp(const int &a,const int &b){ return a>b; } void DFS(int v){ color[v]=1; for(int i=0;i<adj[v].size();i++)if(color[adj[v][i]]==0){ DFS(adj[v][i]); } f[v]=++time; } void DFST(int v){ color[v]=1; for(int i=0;i<adjT[v].size();i++){ int t=adjT[v][i]; if(color[t]==0){ p[t]=root; adj[root].push_back(t); DFST(t); } else if(p[t]!=root&&color[t]==2){ Markadj[p[t]]=false; } } color[v]=2; } void Gscc(){ int i,j; for(i=1;i<=n;i++)if(color[i]==0){ DFS(i); } for(i=1;i<=n;i++){ HashF[f[i]]=i; } sort(f+1,f+n+1,cmp); memset(color,0,sizeof color); memset(p,0,sizeof p); memset(Markadj,true,sizeof Markadj); for(i=1;i<=n;i++){ adj[i].clear(); } for(i=1;i<=n;i++)if(color[HashF[f[i]]]==0){ p[HashF[f[i]]]=root=HashF[f[i]]; adj[root].push_back(root); DFST(HashF[f[i]]); } bool flag=true; for(i=1;i<=n;i++)if(Markadj[p[i]]){ int ans[Size]; for(j=0;j<adj[p[i]].size();j++) ans[j]=adj[p[i]][j]; sort(ans,ans+adj[p[i]].size()); for(j=0;j<adj[p[i]].size();j++){ printf("%d ",ans[j]); } Markadj[p[i]]=false; } printf("/n"); } int main(){ int i,a,b; while(scanf("%d",&n),n){ for(i=1;i<=n;i++){ adj[i].clear(); adjT[i].clear(); } memset(f,0,sizeof f); memset(color,0,sizeof color); memset(HashF,0,sizeof HashF); time=0; scanf("%d",&m); for(i=0;i<m;i++){ scanf("%d%d",&a,&b); adj[a].push_back(b); adjT[b].push_back(a); } Gscc(); } return 0; }