Pagini recente » Cod sursa (job #375747) | Cod sursa (job #1432867) | Cod sursa (job #367470) | Cod sursa (job #467087) | Cod sursa (job #712775)
Cod sursa(job #712775)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in ("sortaret.in");
ofstream out ("sortaret.out");
queue <int> q;
vector<int> a[50200];
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;
}