Cod sursa(job #3202635)

Utilizator AlexandruBenescuAlexandru Benescu AlexandruBenescu Data 12 februarie 2024 07:53:50
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>
#define L 50005
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");

int n, m;
vector <int> G[L], ans;
bool vis[L];

void dfs(int node) {
  vis[node] = true;
  for (auto it : G[node])
    if (!vis[it])
      dfs(it);
  ans.push_back(node);
}

int main() {
  fin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    fin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  for (int i = 1; i <= n; i++) {
    if (vis[i])
      continue;
    dfs(i);
  }
  reverse(ans.begin(), ans.end());

  for (auto it : ans)
    fout << it << " ";
  fout << "\n";
  return 0;
}