Pagini recente » Cod sursa (job #1889097) | Cod sursa (job #422044) | Cod sursa (job #532685) | Istoria paginii runda/oji_simulare_2017_cl10/clasament | Cod sursa (job #2037882)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define NMAX 50005
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n, m;
int viz[NMAX];
vector <int> v[NMAX];
stack <int> st;
void read()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
v[x].push_back(y);
}
}
void sortare_topo(int nod)
{
viz[nod] = 1;
vector<int> :: iterator it;
for (it = v[nod].begin(); it != v[nod].end(); it++)
{
int nr = *it;
if (viz[nr] == 0)
sortare_topo(nr);
}
st.push(nod);
}
void topo()
{
for (int i = 1; i <= n; i++)
if (viz[i] == 0)
sortare_topo(i);
}
void write()
{
while (!st.empty())
{
fout << st.top() << " ";
st.pop();
}
}
int main()
{
read();
topo();
write();
return 0;
}