Pagini recente » Cod sursa (job #3324282) | Cod sursa (job #2474533) | Cod sursa (job #1150176) | Cod sursa (job #1670588) | Cod sursa (job #3353767)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream be("sortaret.in");
ofstream ki("sortaret.out");
int main()
{
int n, m;
be >> n >> m;
vector<int> in(n + 1);
vector<vector<int>> out(n + 1);
for(int i = 0; i < m; i++)
{
int x, y;
be >> x >> y;
in[y]++;
out[x].push_back(y);
}
queue<int> q;
for(int i = 1; i <= n; i++)
{
if(in[i] == 0) q.push(i);
}
while(!q.empty())
{
int x = q.front();
q.pop();
ki << x << " ";
for(int i : out[x])
{
in[i]--;
if(in[i] == 0) q.push(i);
}
}
return 0;
}