Pagini recente » Statistici Ilisuan Iannis Patriciu (iannispatriciu) | Cod sursa (job #212276) | Cod sursa (job #2517452) | Cod sursa (job #193922) | Cod sursa (job #3195240)
#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) {
cout<<head->data<< " ";
head = head->next;
}
// displayGraph();
}