Pagini recente » Cod sursa (job #220225) | Cod sursa (job #3241423) | Cod sursa (job #2539360) | Cod sursa (job #934062) | Cod sursa (job #1645810)
#include <fstream>
#include <list>
#include <stack>
#define w 50001
using namespace std;
list <unsigned int> a[w];
list <unsigned int> l;
stack <unsigned int> st;
unsigned short nrm[w];
void solve()
{
list <unsigned int>::iterator it;
int x;
while (!st.empty())
{
x=st.top();
l.push_back(x);
st.pop();
for (it=a[x].begin();it!=a[x].end();it++)
{
nrm[*it]--;
if (!nrm[*it]) st.push(*it);
}
a[x].clear();
}
}
int main()
{
ifstream f("sortaret.in");
ofstream g("sortaret.out");
unsigned int i,x,y,n,m;
f>>n>>m;
for (i=1;i<=m;i++)
{
f>>x>>y;
a[x].push_back(y);
nrm[y]++;
}
for (i=1;i<=n;i++) if (!nrm[i])st.push(i);
solve();
for (list<unsigned int>::iterator it=l.begin();it!=l.end();it++) g<<*it<<' ';
g<<'\n';
f.close();
g.close();
return 0;
}