Pagini recente » Cod sursa (job #1824283) | Cod sursa (job #2384445) | Cod sursa (job #534651) | Cod sursa (job #992313) | Cod sursa (job #2661892)
#include <fstream>
#include <vector>
#define N_MAX 50001
using namespace std;
int Lista[N_MAX], Viz[N_MAX], N, M;
vector<int> Leg[N_MAX];
void Depth(int x)
{
int STIVA[N_MAX], k = 1;
STIVA[k] = x;
while(k > 0)
{
if(!Leg[STIVA[k]].empty())
{
STIVA[k + 1] = Leg[STIVA[k]].back();
Leg[STIVA[k]].pop_back();
k++;
}
else
{
if(!Viz[STIVA[k]])
Lista[++Lista[0]] = STIVA[k];
Viz[STIVA[k]] = 1;
k--;
}
}
}
int main()
{
ifstream f("sortaret.in");
ofstream g("sortaret.out");
f >> N >> M;
for(int i = 1, x, y; i <= M; i++)
{
f >> x >> y;
Leg[x].push_back(y);
}
for(int i = 1; i <= N; i++)
if(!Viz[i])
Depth(i);
for(int i = Lista[0]; i > 0; i--)
g << Lista[i] << ' ';
return 0;
}