Mai intai trebuie sa te autentifici.
Cod sursa(job #3032645)
Utilizator | Data | 22 martie 2023 16:02:53 | |
---|---|---|---|
Problema | Sortare topologica | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.78 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream in ("sortaret.in");
ofstream out ("sortaret.out");
const int NMAX = 5e4;
vector<int>g[NMAX + 5], gr[NMAX + 5];
int grad[NMAX + 5];
int main()
{
int n, m;
in >> n >> m;
while (m--)
{
int x, y;
in >> x >> y;
g[x].push_back(y);
grad[y]++;
}
queue<int>q;
for (int i=1; i<=n; i++)
if (!grad[i]) q.push(i);
vector<int>ans;
while (!q.empty())
{
int u = q.front();
q.pop();
ans.push_back(u);
for (int v : g[u])
{
grad[v]--;
if (!grad[v])
q.push(v);
}
}
for (int x : ans)
out << x << ' ';
return 0;
}