Pagini recente » Cod sursa (job #849592) | Cod sursa (job #3291101) | Cod sursa (job #688781) | Cod sursa (job #3181681) | Cod sursa (job #2422103)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int N = 50001;
int n,m;
vector<vector<int>>G(N+1);
stack<int> topSort;
void citire()
{
fin>>n>>m;
int x,y;
for(int i=1;i<=m;i++)
{
fin>>x>>y;
G[x].push_back(y);
}
}
void sortare(int x,vector<bool>&viz)
{
viz[x]=true;
for(auto vecin:G[x])
if(!viz[vecin]) sortare(vecin,viz);
topSort.push(x);
}
void rez()
{
vector<bool> viz(N+1,false);
for(int i=1;i<=n;i++)
if(!viz[i]) sortare(i,viz);
while(!topSort.empty())
{
fout<<topSort.top()<<" ";
topSort.pop();
}
}
int main() {
citire();
rez();
return 0;
}