Pagini recente » Cod sursa (job #1430719) | Cod sursa (job #837334) | Cod sursa (job #2088824) | Cod sursa (job #2274001) | Cod sursa (job #894242)
Cod sursa(job #894242)
#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;
for(int i=1; i<=A[x][0]; i++)
{
if(!viz[A[x][i]])
DFS(A[x][i]);
}
list[nr++] = x;
}
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] << ' ';
}