Pagini recente » Cod sursa (job #710522) | Cod sursa (job #1794298) | Cod sursa (job #1629165) | Cod sursa (job #1781920) | Cod sursa (job #2526165)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
const int N = 50001;
const int M = 100001;
int n, m, nr, x, y, st, dr, ns;
int d[N], q[N], vf[2*M], lst[N], urm[2*M], nrp[N], sorttop[N];
bool viz[N];
void adauga(int x, int y)
{
vf[++nr] = y;
urm[nr] = lst[x];
lst[x] = nr;
nrp[y] ++;
}
void dfs(int x)
{
viz[x] = true;
for(int p=lst[x]; p!=0; p=urm[p])
{
y = vf[p];
if(!viz[y])
dfs(y);
}
sorttop[++ns] = x;
}
int main()
{
in>>n>>m;
for(int i=1; i<=m; i++)
{
in>>x>>y;
adauga(x, y);
}
for(int i=1; i<=n; i++)
if(!viz[i])
dfs(i);
for(int i=ns; i>=1; i--)
out<<sorttop[i]<<' ';
return 0;
}