Pagini recente » Cod sursa (job #1088974) | Cod sursa (job #2147206) | Cod sursa (job #1911743) | Cod sursa (job #419949)
Cod sursa(job #419949)
#include <fstream>
#define NMAX 50002
std::ifstream in("sortaret.in");
std::ofstream out("sortaret.out");
int S[NMAX],n,m,v;
bool marc[NMAX];
struct lista{
int nod;
lista *next;
}*L[NMAX];
void adauga(int x,int y)
{
lista *p;
p = new lista;
p->nod = y;
p->next = L[x];
L[x] = p;
}
void read()
{
int i,x,y;
in>>n>>m;
for(i=0;i<m;i++)
{
in>>x>>y;
adauga(x,y);
}
}
void df(int nod)
{
lista *p;
marc[nod]=1;
for(p=L[nod];p!=NULL;p=p->next)
if(marc[p->nod]==0)
df(p->nod);
S[v++]=nod;
}
int main ()
{
int i;
read();
for(i=1;i<=n;i++)
if(marc[i]==0)
df(i);
for(i=n-1;i>=0;i--)
out<<S[i]<<" ";
}