#include <stdio.h>
#include <malloc.h>
#define FIN "topsort.in"
#define DIM 100
typedef struct Node {
int info;
struct Node *next;
} Node;
Node *ListAdj[ DIM ];
Node *solutions;
int explored[DIM];
int nodes, edges;
void readListAdjcency() {
int x, y;
Node *c;
//freopen(FIN, "r", stdin);
scanf("%d %d", &nodes
, &edges
);
while(edges--){
Node
*c
= (Node
*)malloc(sizeof(Node
)); c->info = y;
c->next = ListAdj[x];
ListAdj[x] = c;
}
}
void dfs(int node) {
explored[node] = 1;
Node *c = ListAdj[node];
while(c!=NULL) {
if(!explored[c->info]) {
dfs(c->info);
}
c = c->next;
}
Node
*c2
= (Node
*)malloc(sizeof(Node
)); c2->info = node;
c2->next = solutions;
solutions = c2;
}
void topsort() {
for(int i = 1; i <= nodes; ++i) {
if(!explored[i]) {
dfs(i);
}
}
Node *c = solutions;
while(c) {
c = c->next;
}
}
int main(int argc, char const *argv[]) {
readListAdjcency();
topsort();
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYWxsb2MuaD4KI2RlZmluZSBGSU4gInRvcHNvcnQuaW4iCiNkZWZpbmUgRElNIDEwMAoKdHlwZWRlZiBzdHJ1Y3QgTm9kZSB7CiAgICAgaW50IGluZm87CiAgICAgc3RydWN0IE5vZGUgKm5leHQ7Cn0gTm9kZTsKCk5vZGUgKkxpc3RBZGpbIERJTSBdOwpOb2RlICpzb2x1dGlvbnM7CmludCBleHBsb3JlZFtESU1dOwppbnQgbm9kZXMsIGVkZ2VzOwoKdm9pZCByZWFkTGlzdEFkamNlbmN5KCkgewoKICAgICBpbnQgeCwgeTsKICAgICBOb2RlICpjOwoKICAgICAvL2ZyZW9wZW4oRklOLCAiciIsIHN0ZGluKTsKCiAgICAgc2NhbmYoIiVkICVkIiwgJm5vZGVzLCAmZWRnZXMpOwoKICAgICB3aGlsZShlZGdlcy0tKXsKICAgICAgIHNjYW5mKCIlZCAlZCIsICZ4LCAmeSk7CiAgICAgICBOb2RlICpjID0g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