Pagini recente » Cod sursa (job #2531411) | Cod sursa (job #2366742) | Cod sursa (job #1796986) | Cod sursa (job #523322) | Cod sursa (job #2658396)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int maxi = 100001;
vector<int> a[maxi];
stack<int> stiva;
int vizitat[maxi];
void st(int nod) {
vizitat[nod] = 1;
for(auto i : a[nod])
if(vizitat[i] == 0)
st(i);
stiva.push(nod);
}
void sortareTopologica(int n) {
int i;
for(i = 1; i <= n; ++i)
if(vizitat[i] == 0)
st(i);
while(!stiva.empty()) {
fout << stiva.top() << " ";
stiva.pop();
}
}
int main() {
int n, m, i, x, y;
fin >> n >> m;
for(i = 1; i <= m; ++i) {
fin >> x >> y;
a[x].push_back(y);
}
sortareTopologica(n);
return 0;
}