Pagini recente » Cod sursa (job #1372770) | Cod sursa (job #674298) | Cod sursa (job #2006638) | Cod sursa (job #2246653) | Cod sursa (job #1191503)
#include<iostream>
#include<fstream>
using namespace std;
ifstream f;
ofstream g;
struct lista
{
int inf;
lista *next;
}*t[50000],*q,*p;
int i,j,m,n,k,a[50000],b[50000],nem;
void caut(int p)
{
lista *z;
a[p]=1;
z=t[p];
while(z!=NULL)
{
if(a[z->inf]==0)caut(z->inf);
z=z->next;
}
if(a[p]==1)
{
b[nem]=p;
nem++;
a[p]=2;
}
}
int main()
{
f.open("sortaret.in");
g.open("sortaret.out");
f>>n;
f>>m;
while(m>0)
{
f>>i>>j;
q=new lista;
q->inf=j;
q->next=NULL;
if(t[i]==NULL)t[i]=q;
else
{
p=t[i];
while(p->next!=NULL)p=p->next;
p->next=q;
}
m--;
}
nem=0;
for(i=1;i<=n;i++)
{
if(a[i]==0)caut(i);
}
nem--;
while(nem>=0){g<<b[nem]<<' ';nem--;}
g.close();
return 0;
}