Cod sursa(job #3317288)
| Utilizator | Data | 23 octombrie 2025 08:44:31 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.59 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
int n, m, x, y, vis[50005];
vector<int> v[50005];
stack <int> st;
void dfs (int k) {
vis[k] = 1;
for (auto x: v[k])
if (!vis[x])
dfs(x);
st.push (k);
}
int main()
{
f >> n >> m;
for (int i=1; i<=m; ++i) {
f >> x >> y;
v[x].push_back (y);
}
for (int i=1; i<=n; ++i)
if (!vis[i])
dfs (i);
while (!st.empty()) {
g << st.top() << ' ';
st.pop();
}
return 0;
}
