Pagini recente » Cod sursa (job #935131) | Cod sursa (job #2294619) | Cod sursa (job #939372) | Cod sursa (job #2293871) | Cod sursa (job #2140386)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int N,M,t[50005],k;
bool viz[50005];
vector <int> a[50005];
void dfs(int x)
{
viz[x]=true;
int i;
for(i=0;i<a[x].size();i++)
{
if(viz[a[x][i]]==false) dfs(a[x][i]);
}
t[++k]=x;
}
inline void DFS(int x)
{
int y;
unsigned int i;
viz[x]=true;
for(i=0;i<a[x].size();i++)
{
y=a[x][i];
if(!viz[y]) DFS(y);
}
t[++k]=x;
}
/*
int main()
{
int i,x,y;
ifstream fin("sortaret.in");
fin>>N>>M;
for(i=1;i<=M;i++)
{
fin>>x>>y;
a[x].push_back(y);
}
fin.close();
for(i=1;i<=N;i++)
{
if(viz[i]==0) DFS(i);
}
ofstream fout("sortaret.out");
for(i=k;i>0;i--)
fout<<t[i]<<" ";
fout.close();
return 0;
}
*/
inline void Citirea()
{
int x,y,i;
ifstream fin("sortaret.in");
fin>>N>>M;
for(i=1;i<=M;i++)
{
fin>>x>>y;
a[x].push_back(y);
}
fin.close();
}
int main()
{
int i;
Citirea();
ofstream fout("sortaret.out");
for(i=1;i<=N;i++)
if(!viz[i]) DFS(i);
for(i=k;i>0;i--)
fout<<t[i]<<" ";
fout.close();
return 0;
}