Pagini recente » Cod sursa (job #2483148) | Cod sursa (job #689505) | Cod sursa (job #2405924) | Cod sursa (job #1859530) | Cod sursa (job #2456994)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define pb push_back
const int MAXN = 50010;
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
vector<int> edges[MAXN];
queue<int> nodes;
int grades[MAXN], n, m;
void read() {
int x, y;
fin >> n >> m;
for (int i = 0; i < m; ++i) {
fin >> x >> y;
edges[x].pb(y);
++grades[y];
}
}
void initialize() {
for (int i = 1; i <= n; ++i)
if (!grades[i])
nodes.push(i);
}
void dfs(int node) {
for (auto ind : edges[node]) {
--grades[ind];
if (grades[ind] == 0)
nodes.push(ind);
dfs(ind);
}
}
void solve() {
int x;
while (!nodes.empty()) {
x = nodes.front();
nodes.pop();
dfs(x);
fout << x << " ";
}
}
int main() {
read();
initialize();
solve();
return 0;
}