Pagini recente » Cod sursa (job #128979) | Rating Ungureanu Sebastian (Sebyungureanu22) | Cod sursa (job #126091) | Cod sursa (job #1548564) | Cod sursa (job #2425232)
#include <fstream>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
#define nmax 500500
vector < int > graph[nmax];
stack<int> stiva;
int gard[nmax];
int n, m;
void read() {
f >> n >> m;
for( int i = 0; i < m; i++ ) {
int x, y;
f >> x >> y;
graph[x].push_back(y);
}
}
int viz[nmax];
void dfs(int i) {
viz[i] = 1;
for( auto x: graph[i]) {
if( !viz[x] )
dfs(x);
}
stiva.push(i);
}
int main() {
read();
for( int i = 1; i <= n; i++ )
if(!viz[i]) dfs(i);
while(stiva.size()) {
g << stiva.top() << " ";
stiva.pop();
}
return 0;
}