Pagini recente » Cod sursa (job #3258902) | Borderou de evaluare (job #211312) | Cod sursa (job #3177694) | Cod sursa (job #3144922) | Cod sursa (job #2985106)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
struct Nod
{
int value_node;
Nod *next;
}*adjList[50001],*List;
bool visited[50001];
void AddNeighbour(unsigned int node,unsigned int neighbour)
{
Nod *p=new Nod;
p->value_node=neighbour;
p->next=adjList[node];
adjList[node]=p;
}
void Push(unsigned int node)
{
Nod *p=new Nod;
p->value_node=node;
p->next=List;
List=p;
}
void DFS(unsigned int current_node)
{
visited[current_node]=true;
for(Nod *it=adjList[current_node];it!=NULL;it=it->next)
if(visited[it->value_node]==false)
DFS(it->value_node);
Push(current_node);
}
void PrintList(Nod *&List)
{
while(List->next!=NULL)
{
printf("%u ",List->value_node);
List=List->next;
}
printf("%u ",List->value_node);
}
int main()
{
unsigned int N,M,x,y;
freopen("sortaret.in","r",stdin);
freopen("sortaret.out","w",stdout);
scanf("%u",&N);
scanf("%u",&M);
for(unsigned int i=0;i<M;i++)
{
scanf("%u",&x);
scanf("%u",&y);
AddNeighbour(x,y);
}
for(unsigned int i=1;i<=N;i++)
if(visited[i]==false)
DFS(i);
PrintList(List);
return 0;
}