Pagini recente » Cod sursa (job #3259028) | Rating Chivu Stefan Iulian (aranhil) | Statisticile problemei Fotbal3 | Cod sursa (job #2940639) | Cod sursa (job #1492077)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
void dfs(int v, vector< vector<int> >& graph, vector<int>& ret) {
for (auto& w : graph[v]) {
dfs(w, graph, ret);
}
ret.push_back(v);
}
int main() {
ifstream cin("sortaret.in");
ofstream cout("sortaret.out");
int n, m, x, y;
cin >> n >> m;
vector< vector<int> > graph(n);
vector<int> ret, in(n);
while (m--) {
cin >> x >> y;
graph[x].push_back(y);
in[y]++;
}
for (int i = 0; i < n; i++) {
if (!in[i]) {
dfs(i, graph, ret);
}
}
reverse(begin(ret), end(ret));
for (auto& x : ret) {
cout << x << " ";
}
}