Pagini recente » Cod sursa (job #2964162) | Cod sursa (job #426481) | Cod sursa (job #117415) | Cod sursa (job #453757) | Cod sursa (job #2338991)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
const int MAXN = 5e4;
const int MAXM = 1e5;
int n, m;
vector<int> g[MAXN + 1];
int grad[MAXN + 1];
queue<int> nodes;
int main() {
in >> n >> m;
int x, y;
for (int i = 1; i <= m; ++ i) {
in >> x >> y;
g[x].push_back(y);
++ grad[y];
}
for (int i = 1; i <= n; ++ i) {
if (grad[i] == 0) {
nodes.push(i);
break;
}
}
while (nodes.size()) {
for (auto x : g[nodes.front()]) {
-- grad[x];
if (grad[x] == 0) nodes.push(x);
}
out << nodes.front() << ' ';
nodes.pop();
}
return 0;
}