Pagini recente » Cod sursa (job #774801) | Cod sursa (job #3280410) | Cod sursa (job #2509514) | Cod sursa (job #548885) | Cod sursa (job #1452996)
#include<fstream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
ofstream fout("sortaret.out");
ifstream fin("sortaret.in");
const long int MMAX = 100001;
int n, m, varf;
int viz[50001];
vector<int> v[MMAX];
queue<int> q;
void citire()
{
int x, y;
fin >> n >> m;
for(int i=1; i<=m; i++) {
fin >> x >> y;
v[x].push_back(y);
viz[y] = 1;
}
for(int i=1; i<=n; i++)
if(!viz[i]) varf = i;
memset(viz, 0, sizeof(viz));
}
void topsort(int x)
{
q.push(x);
viz[x] = 1;
for(int i=0; i<v[x].size(); i++) {
if(!viz[v[x][i]]) {
viz[v[x][i]] = 1;
topsort(v[x][i]);
}
}
}
void afis()
{
int yolo;
while(!q.empty()) {
yolo = q.front();
q.pop();
fout << yolo << ' ';
}
}
int main()
{
citire();
topsort(varf);
for(int i=1; i<=n; i++) {
if(!viz[i])
topsort(i);
}
afis();
fin.close();
fout.close();
return 0;
}