Pagini recente » Cod sursa (job #2978335) | Cod sursa (job #2199301) | Cod sursa (job #2390878) | Cod sursa (job #2111759) | Cod sursa (job #3239020)
#include<fstream>
#include<vector>
#include<bitset>
std::ifstream fin("sortaret.in");
std::ofstream fout("sortaret.out");
const int NMAX=50005;
std::vector<int>G[NMAX], ans;
std::bitset<NMAX>f;
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)
{
ans.push_back(node);
for(auto it:G[node])
{
if(!f[it])
{
f[it]=true;
topological_sort(it);
}
}
}
void displayAns()
{
for(auto it:ans)
fout<<it<<' ';
}
int main()
{
read();
f[first]=true;
topological_sort(first);
displayAns();
return 0;
}