Pagini recente » Cod sursa (job #2289888) | Cod sursa (job #1990330) | Cod sursa (job #1382800) | Cod sursa (job #1539871) | Cod sursa (job #2087325)
#include <fstream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n, m;
map<int, int> deg;
vector<int> G[50001];
queue<int> Q;
void read() {
fin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
fin >> x >> y;
deg[y]++;
G[x].push_back(y);
}
}
void solve() {
for (int x = 1; x <= n; x++) {
if (deg[x] == 0) {
Q.push(x);
fout << x << " ";
}
}
while (!Q.empty()) {
int x = Q.front();
for (int i : G[x]) {
deg[i]--;
if (deg[i] == 0) {
Q.push(i);
fout << i << " ";
}
}
Q.pop();
}
}
int main() {
read();
solve();
return 0;
}