Cod sursa(job #3205149)

Utilizator TrifoitaBejenescu-Babusanu Stefan Trifoita Data 18 februarie 2024 22:03:43
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");

int matrix[100][100], used[100];
int n,m;

void read() {
  fin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x,y;
    fin >> x >> y;
    matrix[x][y] = 1;
  }
}

void dfs(int node) {
  fout << node << " ";
  used[node] = 1;
  for (int i = 1; i <= n; i++)
    if (matrix[node][i] && !used[i])
      dfs(i);
}

// void read() {
//   fin >> n >> m;
//   for (int i = 1; i <= m; i++) {
//     int i, j; fin >> i >> j;
//     matrix[i][j] = 1;
//   }
// }
//
// void dfs() {
//
// }

int main() {
  read();
  for (int node = 1; node <= n; node++)
    if (!used[node])
      dfs(node);
  return 0;
}