Pagini recente » Cod sursa (job #647948) | Cod sursa (job #1532069) | Cod sursa (job #387635) | Cod sursa (job #2716636) | Cod sursa (job #3156883)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
const int NMAX = 50001;
const int MMAX = 100001;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
vector<int> L[NMAX];
queue <int> Q;
int grad[NMAX];
int N, M;
void citire()
{
int x, y;
f >> N >> M;
for(int i = 1 ; i <= M ; i++)
{
f >> x >> y;
L[x].push_back(y);
L[y].push_back(x);
grad[y]++;
}
}
void TopSort()
{
for(int i = 1 ; i <= N ; i++)
if(grad[i] == 0)
Q.push(i);
while(!Q.empty())
{
int x = Q.front();
Q.pop();
g << x << ' ';
for(const auto &y : L[x])
{
grad[y]--;
if (grad[y] == 0)
Q.push(y);
}
}
}
int main()
{
citire();
TopSort();
return 0;
}