#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define fin "strongly.in"
#define STACK_MAX 100
#define DIM 100
typedef struct Node {
int info;
struct Node *next;
} Node;
int nodes,
edges,
comp;
Node *L[DIM],
*L_Reverse[DIM],
*Result[DIM];
int explored[DIM];
#include <stdio.h>
#define STACK_MAX 100
#define FIN "scarface.in"
struct Stack {
int size;
char data[ STACK_MAX ];
};
typedef struct Stack Stack;
void Stack_Init(Stack *S) {
S->size = 0;
}
int Stack_Empty(Stack *S) {
return S->size == 0;
}
void Stack_Push(Stack *S, int val) {
if( S->size < STACK_MAX ) {
S->data[S->size++] = val;
} else fprintf(stderr
, "Err: Stack Full!\n");
}
void Stack_Pop(Stack *S) {
if( S
->size
== 0 ) fprintf(stderr
, "Error: Stack Empty!");
else S->size--;
}
int Stack_Top(Stack *S) {
if(S->size == 0) {
fprintf(stderr
, "Error: Stack Empty!"); return -1;
}
return S->data[ S->size - 1];
}
Stack S;
void readGraph() {
int x, y;
//freopen(fin, "r", stdin);
scanf("%d %d", &nodes
, &edges
); while(edges--) {
Node
*c
= (Node
*)malloc(sizeof(Node
)); c->info = y;
c->next = L[x];
L[x] = c;
Node
*c2
= (Node
*)malloc(sizeof(Node
)); c2->info = x;
c2->next = L_Reverse[y];
L_Reverse[y] = c2;
}
}
void dfs(int node) {
explored[node] = 1;
Stack_Push(&S, node);
Node *c = L[node];
while(c) {
if(!explored[c->info]) {
dfs(c->info);
}
c = c->next;
}
}
void dfs_r(int node) {
explored[ node ] = 1;
Node
*head
= (Node
*)malloc(sizeof(Node
)); head->info = node;
head->next = Result[comp];
Result[comp] = head;
Node *c = L_Reverse[node];
while(c) {
if(!explored[c->info]) {
dfs_r(c->info);
}
c = c->next;
}
}
void kosaraju() {
Stack_Init(&S);
for(int node = 1; node <= nodes; ++node) {
if(!explored[node]) {
dfs(node);
}
}
memset(explored
, 0, sizeof(explored
));
int vertex;
comp = 0;
while(!Stack_Empty(&S)) {
vertex = Stack_Top(&S);
Stack_Pop(&S);
if(!explored[vertex]) {
comp++;
dfs_r(vertex);
}
}
for(int i = 1; i <= comp; ++i) {
Node *k = Result[i];
while( k ) {
k = k -> next;
}
}
}
int main(int argc, char const *argv[]) {
readGraph();
kosaraju();
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYWxsb2MuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgojZGVmaW5lIGZpbiAic3Ryb25nbHkuaW4iCiNkZWZpbmUgU1RBQ0tfTUFYIDEwMAojZGVmaW5lIERJTSAxMDAKCnR5cGVkZWYgc3RydWN0IE5vZGUgewogICAgICAgIGludCBpbmZvOwogICAgICAgIHN0cnVjdCBOb2RlICpuZXh0Owp9IE5vZGU7CgppbnQgbm9kZXMsCiAgICBlZGdlcywKICAgIGNvbXA7Ck5vZGUgKkxbRElNXSwKICAgICAqTF9SZXZlcnNlW0RJTV0sCiAgICAgKlJlc3VsdFtESU1dOwoKaW50IGV4cGxvcmVkW0RJTV07CgojaW5jbHVkZSA8c3RkaW8uaD4KI2RlZmluZSBTVEFDS19NQVggMTAwCiNkZWZpbmUgRklOICJzY2FyZmFjZS5pbiIKCnN0cnVjdCBTdGFjayB7CgoJaW50IHNpemU7CgljaGFyIGRhdGFbIFNUQUNLX01BWCBdOwp9OwoKdHlwZWRlZiBzdHJ1Y3QgU3RhY2sgU3RhY2s7Cgp2b2lkIFN0YWNrX0luaXQoU3RhY2sgKlMpIHsKCiAgICAgUy0+c2l6ZSA9IDA7Cn0KCmludCBTdGFja19FbXB0eShTdGFjayAqUykgewoKICAgICByZXR1cm4gUy0+c2l6ZSA9PSAwOwp9Cgp2b2lkIFN0YWNrX1B1c2goU3RhY2sgKlMsIGludCB2YWwpIHsKCiAgICAgaWYoIFMtPnNpemUgPCBTVEFDS19NQVggKSB7CgogICAgICAgICBTLT5kYXRhW1MtPnNpemUrK10gPSB2YWw7CgogICAgIH0gZWxzZSBmcHJpbnRmKHN0ZGVyciwgIkVycjogU3RhY2sgR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mluZm9dKSB7CgogICAgICAgICAgZGZzKGMtPmluZm8pOwogICAgICAgfQoKICAgICAgIGMgPSBjLT5uZXh0OwogICAgIH0KfQoKdm9pZCBkZnNfcihpbnQgbm9kZSkgewoKICAgICBleHBsb3JlZFsgbm9kZSBdID0gMTsKCiAgICAgTm9kZSpoZWFkID0gKE5vZGUqKW1hbGxvYyhzaXplb2YoTm9kZSkpOwogICAgIGhlYWQtPmluZm8gPSBub2RlOwogICAgIGhlYWQtPm5leHQgPSBSZXN1bHRbY29tcF07CiAgICAgUmVzdWx0W2NvbXBdID0gaGVhZDsKCiAgICAgTm9kZSAqYyA9IExfUmV2ZXJzZVtub2RlXTsKCiAgICAgd2hpbGUoYykgewoKICAgICAgIGlmKCFleHBsb3JlZFtjLT5pbmZvXSkgewoKICAgICAgICAgIGRmc19yKGMtPmluZm8pOwogICAgICAgfQoKICAgICAgIGMgPSBjLT5uZXh0OwogICAgIH0KfQoKdm9pZCBrb3NhcmFqdSgpIHsKCiAgU3RhY2tfSW5pdCgmUyk7CgogIGZvcihpbnQgbm9kZSA9IDE7IG5vZGUgPD0gbm9kZXM7ICsrbm9kZSkgewoKICAgICAgaWYoIWV4cGxvcmVkW25vZGVdKSB7CgogICAgICAgIGRmcyhub2RlKTsKICAgICAgfQogIH0KCiAgIG1lbXNldChleHBsb3JlZCwgMCwgc2l6ZW9mKGV4cGxvcmVkKSk7CgogICBpbnQgdmVydGV4OwoKICAgY29tcCA9IDA7CgogICB3aGlsZSghU3RhY2tfRW1wdHkoJlMpKSB7CgogICAgICAgICB2ZXJ0ZXggPSBTdGFja19Ub3AoJlMpOwogICAgICAgICAKICAgICAgICAgU3RhY2tfUG9wKCZTKTsKCiAgICAgICAgIGlmKCFleHBsb3JlZFt2ZXJ0ZXhdKSB7CgogICAgICAgICAgICBjb21wKys7CgogICAgICAgICAgICBkZnNfcih2ZXJ0ZXgpOwogICAgICAgICB9CiAgIH0KCiAgIGZvcihpbnQgaSA9IDE7IGkgPD0gY29tcDsgKytpKSB7CgogICAgICAgcHJpbnRmKCJDb21wb25lbnQgJWQ6IiwgaSk7CgogICAgICAgTm9kZSAqayA9IFJlc3VsdFtpXTsKCiAgICAgICB3aGlsZSggayApIHsKCiAgICAgICAgIHByaW50ZigiJWQgIiwgay0+aW5mbyk7CgogICAgICAgICBrID0gayAtPiBuZXh0OwogICAgICAgfQoKICAgICAgIHByaW50ZigiXG4iKTsKCiAgIH0KfQoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIgY29uc3QgKmFyZ3ZbXSkgewoKICByZWFkR3JhcGgoKTsKCiAga29zYXJhanUoKTsKCiAgcmV0dXJuIDA7Cn0K