Pagini recente » Cod sursa (job #1723743) | Statistici UNIBUC TANASESCU POPESCU MUSUROI (Unibuc_Forza_Dragulici) | Profil andrici_cezar | Cod sursa (job #786525) | Cod sursa (job #2224757)
#include <bits/stdc++.h>
using namespace std;
void dfs(const vector<vector<int>>& graph, int node, vector<bool>& visited, vector<int>& topSort){
visited[node] = true;
for (int x: graph[node])
if (!visited[x])
dfs(graph, x, visited, topSort);
topSort.push_back(node);
}
int main() {
// ifstream cin("data.txt");
ifstream cin("sortaret.in");
ofstream cout("sortaret.out");
ios_base::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<vector<int>> graph;
graph.resize(N);
while (M--){
int a, b;
cin >> a >> b;
a--; b--;
graph[a].push_back(b);
}
vector<bool> visited(N);
vector<int> topSort;
for (int i = 0; i < N; i++)
if (!visited[i])
dfs(graph, i, visited, topSort);
reverse(topSort.begin(), topSort.end());
for (int x: topSort)
cout << x + 1 << " ";
cout << endl;
return 0;
}