Cod sursa(job #2425287)
| Utilizator | Data | 24 mai 2019 18:02:39 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.51 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
int n, m, x, y, viz[100100], st[100100], vf;
vector<int> v[100100];
void dfs(int x) {
viz[x] = 1;
for (auto y : v[x])
if (!viz[y])
dfs(y);
st[++vf] = x;
}
int main() {
in >> n >> m;
for (int i = 1; i <= m; i++) {
in >> x >> y;
v[x].push_back(y);
}
for (int i = 1; i <= n; i++)
if (!viz[i])
dfs(i);
for (; vf; vf--)
out << st[vf] << ' ';
return 0;
}
