Pagini recente » Cod sursa (job #2767195) | Cod sursa (job #116219) | Cod sursa (job #2321077) | Cod sursa (job #1156478) | Cod sursa (job #2421665)
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
queue<int> coada;
vector<int> deg(50005, 0);
vector<vector<int>> G(50005);
int main()
{
int n, m, x, y;
f >> n >> m;
for(int i = 1; i <= m; i++)
{
f >> x >> y;
deg[y]++;
G[x].push_back(y);
}
for(int i = 1; i <= n; i++)
{
if(deg[i] == 0)
coada.push(i);
}
while(!coada.empty())
{
int node = coada.front();
coada.pop();
g << node << " ";
// for(int i = 0; i <G[node].size(); i++)
for(auto i:G[node])
{
deg[i] --;
if(deg[i] == 0)
coada.push(i);
}
}
return 0;
}