Pagini recente » Cod sursa (job #1359414) | Cod sursa (job #1732998) | Cod sursa (job #2831102) | Cod sursa (job #2898873) | Cod sursa (job #832207)
Cod sursa(job #832207)
#include<fstream>
using namespace std;
#define nmax 50010
ifstream f("sortaret.in");
ofstream g("sortaret.out");
struct nod
{
int nr;
nod *adr;
} *asc[nmax],*desc[nmax];
int n,m,ok[nmax],urc[nmax];
void citire()
{
int a,b;
f>>n>>m;
for(int i=1;i<=m;i++)
{
f>>a>>b;
nod *p=new nod;
p->nr=b;
p->adr=desc[a];
desc[a]=p;
p=new nod;
p->nr=a;
p->adr=asc[b];
asc[b]=p;
}
}
void rez(int x)
{
nod *p;
if(asc[x]!=NULL && ok[x]==0)
{
urc[x]=1;
for(p=asc[x];p!=NULL;p=p->adr)
if(ok[p->nr]==0)
rez(p->nr);
}
g<<x<<' ';
ok[x]=1;
for(p=desc[x];p!=NULL;p=p->adr)
if(urc[p->nr]==0)
rez(p->nr);
}
int main()
{
citire();
for(int i=1;i<=n;i++)
if(ok[i]==0)
rez(i);
return 0;
}