Pagini recente » preONI 2008 - Clasament Runda 1, Clasele 11-12 | Cod sursa (job #768627) | Cod sursa (job #815496) | Cod sursa (job #2324112) | 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;
}