Pagini recente » Cod sursa (job #1352870) | Cod sursa (job #1002530) | Cod sursa (job #26518) | Cod sursa (job #1405759) | Cod sursa (job #3248588)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n, m;
vector<set<int> > v;
vector<int> b;
vector<int> l;
void dfs(int i)
{
b[i]=1;
for (set<int>::iterator it=v[i].begin(); it!=v[i].end(); it++){
if (!b[*it]){dfs(*it);l.push_back(*it);}
}
}
int main()
{
fin>>n>>m;
v.resize(n+1);
b.resize(n+1);
for (int i=0; i<m; i++){
int x, y;
fin>>x>>y;
v[x].insert(y);
}
for (int i=1; i<=n; i++){
if (!b[i]){dfs(i); l.push_back(i);};
}
for (int i=l.size()-1; i>=0; i--)fout<<l[i]<<" ";
return 0;
}