Pagini recente » Cod sursa (job #2801639) | Cod sursa (job #1737033) | Cod sursa (job #2228153) | Cod sursa (job #1382604) | Cod sursa (job #953061)
Cod sursa(job #953061)
#include<fstream>
#include<vector>
#include<stack>
using namespace std;
int N,M;
vector<int> G[50000];
vector<int> R[50000];
stack<int> S;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
void Read()
{
f>>N>>M;
for(int i=0;i<M;i++)
{
int x,y;
f>>x>>y;
G[x].push_back(y);
}
}
int IsLeaf(int x)
{
return !G[x].size();
}
void TopologicalSort(int x)
{
for(vector<int>::iterator it=G[x].begin();it!=G[x].end();it++)
{
if(IsLeaf(*it))
{
S.push(*it);
}
else
{
TopologicalSort(*it);
S.push(*it);
}
}
}
void Write()
{
g<<1<<' ';
while(!S.empty())
{
g<<S.top()<<" ";
S.pop();
}
g<<'\n';
}
int main()
{
Read();
TopologicalSort(1);
Write();
return 0;
}