Pagini recente » Cod sursa (job #966637) | Cod sursa (job #2235368) | Cod sursa (job #2158766) | Cod sursa (job #2236406) | Cod sursa (job #1565803)
#include <fstream>
#include <queue>
using namespace std;
const int MAX = 50004;
int grad[MAX],n,m;
queue <int> coada[MAX]; queue <int> lista;
ofstream q("sortaret.out");
void read();
void sort();
void write();
int main()
{
read();
sort();
return 0;
}
void sort()
{
for (int i = 1; i <=n; i++)
{
if (grad[i] == 0)
{
lista.push(i);
}
}
while (!lista.empty())
{
int x = lista.front();
q<<x<<" ";
lista.pop();
while (!coada[x].empty())
{
int y = coada[x].front();
coada[x].pop();
grad[y]--;
if (grad[y]==0)
{
lista.push(y);
}
}
}
q.close();
}
void read()
{
ifstream f("sortaret.in");
f>>n>>m;
for ( ; m > 0; m--)
{
int x,y;
f >> x >> y;
coada[x].push(y);
grad[y]++;
}
f.close();
}