Pagini recente » Cod sursa (job #582323) | Cod sursa (job #3275817) | Cod sursa (job #1582547) | Cod sursa (job #21661) | Cod sursa (job #2422270)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
using namespace std;
#define MAX_NODES 50005
vector<int> graf[MAX_NODES];
int vizitat[MAX_NODES];
queue<int> coada;
vector<int> raspuns;
int grad[MAX_NODES];
ifstream f("sortaret.in");
ofstream g("sortaret.out");
int main()
{
int N, M;
f>>N>>M;
for(int i = 1; i <= M; i++)
{
int x, y;
f>>x>>y;
grad[y] ++;
graf[x].push_back(y);
}
for(int i = 1; i <= N; i++)
{
if(grad[i] == 0)
{
coada.push(i);
}
}
while(!coada.empty())
{
int x = coada.front();
coada.pop();
for(int i = 0; i < graf[x].size(); i++)
{
grad[graf[x][i]] --;
if(grad[graf[x][i]] == 0)
{
coada.push(graf[x][i]);
}
}
raspuns.push_back(x);
}
for(int i = 0; i < raspuns.size(); i++)
{
g<<raspuns[i]<<" ";
}
return 0;
}