Pagini recente » Cod sursa (job #727808) | Cod sursa (job #2787887) | Cod sursa (job #1045534) | Cod sursa (job #2749207) | Cod sursa (job #2422096)
#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<=n;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()
{
citire();
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() {
rez();
// for(int i=1;i<=n;i++) {
// cout << i << ": ";
// for (auto vecin:G[i])
// fout << vecin << " ";
// fout<<'\n';
// }
return 0;
}