Pagini recente » Cod sursa (job #2058681) | Cod sursa (job #1993877) | Cod sursa (job #2199670) | Cod sursa (job #261497) | Cod sursa (job #712771)
Cod sursa(job #712771)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in ("sortaret.in");
ofstream out ("sortaret.out");
queue <int> q;
vector<int> a[8000];
int m,n,pred[50020];
void citire ()
{
int x,y;
for (int i=1; i<=m; i++)
{
in>>x>>y;
a[x].push_back(y);
pred[y]++;
}
}
void bfs ()
{
int x,y;
for (int i=1 ; i<=n; i++)
{
if (pred[i] == 0)
{
q.push(i);
//out<<i<<" ";
}
}
while (!q.empty())
{
x = q.front ();
out<<x<<" ";
q.pop ();
for (size_t i=0 ; i<a[x].size() ; i++)
{
y = a[x][i];
pred[y]--;
if(pred[y] == 0)
{
q.push(y);
//out<<y<<" ";
}
}
}
}
int main ()
{
in>>n>>m;
citire ();
bfs ();
return 0;
}