Cod sursa(job #3195239)

Utilizator andrei_botorogeanuBotorogeanu Andrei andrei_botorogeanu Data 20 ianuarie 2024 11:57:04
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include<iostream>
#define fin "sortaret.in"
#define fout "sortaret.out"
#define size 100
using namespace std;

struct Node {
	int data;
	struct Node *next;
};
struct Node *Graph[size];
struct Node *head = NULL;
int visited[size],
	nodes,
	edges;

void CreateGraph()
{
	int i, j;
	freopen(fin, "r", stdin);
	cin>>nodes>>edges;
	for(int k=1; k<=edges; k++) {
		cin>>i>>j;
		struct Node *nod = new Node;
		nod->data = j;
		nod->next = Graph[ i ];
		Graph[ i ] = nod;
	}
}
void displayGraph()
{
	struct Node *c;
	for(int node=1; node<=nodes; node++) {
		c = Graph[node];
		while( c!=nullptr ) {
			printf("%d ",c->data);
			c = c->next;
		} 
		printf("\n");
	}
}
void dfs(int node) {
	struct Node *c, *p;
	visited[ node ] = 1;
	for(c = Graph[node]; c!=nullptr; c = c->next) {
		if( !visited[c->data] ) {
			dfs(c->data);
		}
	}
	p = new Node;
	p->data = node;
	p->next = head;
	head = p ;
}
int main()
{
	freopen(fout, "w", stdout);
	CreateGraph();
	for(int node=1; node<=nodes; node++) {
		if( visited[node] == 0 ) {
			dfs(node);
		}
	}
	while(head) {
		fout<<head->data<< " ";
		head = head->next;
	}
//	displayGraph();
}