Pagini recente » Borderou de evaluare (job #1432135) | Cod sursa (job #915572) | Cod sursa (job #815450) | Borderou de evaluare (job #992016) | Cod sursa (job #3239023)
#include<fstream>
#include<vector>
#include<bitset>
#include<stack>
std::ifstream fin("sortaret.in");
std::ofstream fout("sortaret.out");
const int NMAX=50005;
std::vector<int>G[NMAX];
std::bitset<NMAX>f;
std::stack<int>ans;
int n, m, first;
void read()
{
fin>>n>>m;
for(int i=0; i<m; ++i)
{
int from, to;
fin>>from>>to;
if(!first)
first=from;
G[from].push_back(to);
}
}
void topological_sort(int node)
{
for(auto it:G[node])
{
if(!f[it])
{
f[it]=true;
topological_sort(it);
}
}
ans.push(node);
}
void displayAns()
{
while(!ans.empty())
{
fout<<ans.top()<<' ';
ans.pop();
}
}
int main()
{
read();
for(int i=1; i<=n; ++i)
{
if(!f[i])
{
f[i]=true;
topological_sort(i);
}
}
displayAns();
return 0;
}