Pagini recente » Cod sursa (job #386627) | Cod sursa (job #2380587) | Cod sursa (job #860671) | Cod sursa (job #1686292) | Cod sursa (job #2723803)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int N = 5e5;
int d[N],nrp[N];
vector <int> a[N];
queue <int> q;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
void bfs()
{
while(!q.empty())
{
int x = q.front();
out << x << " ";
q.pop();
for(auto y:a[x])
{
nrp[y]--;
if(nrp[y] == 0)
{
q.push(y);
}
}
}
}
int main()
{
int n,m;
in >> n >> m;
for(int i = 1; i <= m; i++)
{
int x,y;
in >> x >>y;
a[x].push_back(y);
nrp[y]++;
}
for(int i=1; i <= n; i++)
{
if(nrp[i] == 0)
{
q.push(i);
}
}
bfs();
in.close();
out.close();
return 0;
}