Pagini recente » Cod sursa (job #1256942) | Cod sursa (job #1710436) | Cod sursa (job #1852877) | Cod sursa (job #2500431) | Cod sursa (job #1575615)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int nmax = 50005;
vector <int> g[nmax], sol;
bool viz[nmax];
void dfs(int dad)
{
int i, son;
viz[dad]=true;
sol.push_back(dad);
for(i=0; i<g[dad].size(); i++)
{
son=g[dad][i];
if(!viz[son]) dfs(son);
}
}
int main()
{
ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");
ios_base::sync_with_stdio(false);
int n, m, i, x, y;
fin >> n >> m;
for(i=1; i<=m; i++)
{
fin >> x >> y;
g[x].push_back(y);
}
for(i=1; i<=n; i++)
if(viz[i]==false)
dfs(i);
for(i=0; i<sol.size(); i++)
fout << sol[i] << " ";
fin.close();
fout.close();
return 0;
}