Pagini recente » Cod sursa (job #3234270) | Cod sursa (job #887180) | Cod sursa (job #729220) | Cod sursa (job #986179) | Cod sursa (job #265602)
Cod sursa(job #265602)
#include <stdio.h>
#define NMAX 50010
typedef struct nod
{
int v;
nod *urm;
} *pnod;
pnod list[NMAX];
int n, m, grad[NMAX];
void baga(int x, int y)
{
pnod aux;
aux = new nod;
aux -> v = y;
aux -> urm = list[x];
list[x] = aux;
}
void read()
{
int x, y;
scanf("%d %d", &n, &m);
while(m--)
{
scanf("%d %d", &x, &y);
++grad[y];
baga(x, y);
}
}
void bf()
{
int inc = 1, sf = 0;
int c[NMAX], next;
pnod it;
int i, x;
for(i = 1; i <= n; ++i)
if(!grad[i])
c[++sf] = i;
while(inc <= sf)
{
x = c[inc++];
printf("%d ", x);
for(it = list[x]; it != NULL; it = it -> urm)
{
next = it -> v;
if(--grad[next] == 0)
c[++sf] = next;
}
}
}
int main()
{
freopen("sortaret.in", "r", stdin);
freopen("sortaret.out", "w", stdout);
read();
bf();
return 0;
}