Pagini recente » Cod sursa (job #1218828) | Cod sursa (job #1909778) | Cod sursa (job #292616) | Cod sursa (job #834994) | Cod sursa (job #2582078)
#include <fstream>
#include <iostream>
using namespace std;
ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");
int nod_nb, arc_nb;
int dfs(int level, int nod, int** legaturi, int* solutie)
{
solutie[level] = nod;
for (int i = 1; i <= nod_nb; i++)
if (legaturi[nod][i])
level = dfs(level + 1, i, legaturi, solutie);
return level;
}
int main()
{
fin >> nod_nb >> arc_nb;
int* solutie;
solutie = (int*)malloc(sizeof(int) * (nod_nb + 1));
int** legaturi;
legaturi = (int**)malloc(sizeof(int*) * (nod_nb + 1));
for (int i = 0; i <= nod_nb; i++)
*(legaturi + i) = (int*)malloc(sizeof(int) * (nod_nb + 1));
for (int i = 0; i <= nod_nb; i++)
for (int j = 0; j <= nod_nb; j++)
legaturi[i][j] = 0;
while (arc_nb--)
{
int parent, son;
fin >> parent >> son;
legaturi[parent][son] = 1;
}
dfs(1, 1, legaturi, solutie);
for (int i = 1; i <= nod_nb; i++)
fout << solutie[i] << ' ';
return 0;
}