Pagini recente » Cod sursa (job #995673) | Cod sursa (job #226761) | Cod sursa (job #2824470) | Cod sursa (job #711501) | Cod sursa (job #894225)
Cod sursa(job #894225)
#include <iostream>
#include <fstream>
#define nMax 50001
bool viz[nMax] = {0};
int n, m, nr = 0;
int A[nMax][1000] = {0};
int list[nMax];
using namespace std;
void DFS(int x)
{
viz[x] = true;
list[nr++] = x;
for(int i=1; i<=A[x][0]; i++)
{
if(!viz[A[x][i]])
DFS(A[x][i]);
}
}
int main()
{
int x, y;
fstream f, g;
f.open("sortaret.in", ios::in);
g.open("sortaret.out", ios::out);
f >> n >> m;
for(int i=1; i<=m; i++)
{
f >> x >> y;
A[x][0]++;
A[x][A[x][0]] = y;
}
for(int i=1; i<=n; i++)
if(!viz[i]) DFS(i);
for(int i=0; i<nr; i++)
g << list[i] << ' ';
}