Pagini recente » Cod sursa (job #38309) | Cod sursa (job #2204962) | Cod sursa (job #2555510) | Cod sursa (job #530205) | Cod sursa (job #2641886)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
int n, m;
int deg[50001];
vector < int > graph[50001], v;
queue < int > q;
int main()
{
f >> n >> m;
for (int i=1; i<=m; i++)
{
int x, y; f >> x >> y;
graph[x].push_back(y);
deg[y]++;
}
for (int i=1; i<=n; i++)
if (!deg[i])
q.push(i);
while (!q.empty())
{
int node = q.front();
q.pop();
v.push_back(node);
for (auto it : graph[node])
{
deg[it]--;
if (!deg[it])
q.push(it);
}
}
for (auto it : v)
g << it << " ";
return 0;
}