Pagini recente » Cod sursa (job #2723742) | Cod sursa (job #2723967) | Cod sursa (job #1840992) | Cod sursa (job #2754380) | Cod sursa (job #1575041)
#include <iostream>
#include <fstream>
#define MAXN 50005
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n, m;
int grad[MAXN];
struct cell
{
int info;
cell *next;
} *gr[MAXN], *Q;
void add(int a, int b)
{
cell *q = new cell;
q->info = b;
q->next = gr[a];
gr[a] = q;
}
void push(int x)
{
cell *q = new cell;
q->info = x;
q->next = NULL;
if (Q == NULL)
{
Q = q;
return;
}
cell *p = Q;
while (p->next)
p = p->next;
p->next = q;
}
void pop()
{
cell *aux = Q;
Q = Q->next;
delete aux;
}
void sortTop()
{
for (int i = 1; i <= n; ++i)
if (grad[i] == 0)
{
fout << i << ' ';
push(i);
}
while (Q)
{
cell *q = gr[Q->info];
pop();
while (q)
{
grad[q->info]--;
if (grad[q->info] == 0)
{
fout << q->info << ' ';
push(q->info);
}
q = q->next;
}
}
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
int a, b;
fin >> a >> b;
add(a, b);
grad[b]++;
}
sortTop();
return 0;
}