Pagini recente » Cod sursa (job #1896486) | Cod sursa (job #952518) | Cod sursa (job #1331951) | Cod sursa (job #467878) | Cod sursa (job #2570863)
#include <bits/stdc++.h>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
const int MAX = 5e4 + 5;
int n, m;
int grad[MAX];
vector<int> G[MAX], ans;
queue<int> q;
int main()
{
f >> n >> m;
for(int i = 1; i <= m; ++i)
{
int x, y;
f >> x >> y;
G[x].push_back(y);
++grad[y];
}
for(int i = 1; i <= n; ++i)
if(grad[i] == 0)
q.push(i);
while(!q.empty())
{
int u = q.front();
ans.push_back(u);
q.pop();
for(int v : G[u])
{
--grad[v];
if(grad[v] == 0)
q.push(v);
}
}
for(int i : ans)
g << i << " ";
return 0;
}