Pagini recente » Cod sursa (job #2939311) | Cod sursa (job #2773229) | Cod sursa (job #2711628) | Cod sursa (job #3179115) | Cod sursa (job #3162244)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int MAXN = 50005;
int N, M;
vector<int> graph[MAXN];
int in_degree[MAXN];
void topologicalSort() {
queue<int> q;
vector<int> result;
for (int i = 1; i <= N; ++i) {
if (in_degree[i] == 0) {
q.push(i);
}
}
while (!q.empty()) {
int node = q.front();
q.pop();
result.push_back(node);
for (int neighbor : graph[node]) {
in_degree[neighbor]--;
if (in_degree[neighbor] == 0) {
q.push(neighbor);
}
}
}
// Afisarea sortarii topologice
for (int node : result) {
cout << node << ' ';
}
cout << '\n';
}
int main() {
ifstream fin("sortaret.in");
fin >> N >> M;
for (int i = 0; i < M; ++i) {
int X, Y;
fin >> X >> Y;
graph[X].push_back(Y);
in_degree[Y]++;
}
fin.close();
topologicalSort();
return 0;
}