Pagini recente » Cod sursa (job #2813010) | Cod sursa (job #628003) | Cod sursa (job #521076) | Cod sursa (job #1833265) | Cod sursa (job #3213830)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int MAX_SIZE = 5 * 1e4;
int n, m;
vector<int> graph[MAX_SIZE + 1];
vector<int> topologicalSorted;
void depthFirstSearch(int start) {
vector<bool> visited(MAX_SIZE + 1, false);
stack<int> nodes;
nodes.push(start);
while (!nodes.empty()) {
int currentNode = nodes.top();
nodes.pop();
if (!visited[currentNode]) {
visited[currentNode] = true;
topologicalSorted.push_back(currentNode);
for (int neighbor : graph[currentNode]) {
if (!visited[neighbor]) {
nodes.push(neighbor);
}
}
}
}
}
int main() {
fin >> n >> m;
for (int i = 0; i < m; ++i) {
int x, y;
fin >> x >> y;
graph[x].push_back(y);
}
depthFirstSearch(1);
for (int node : topologicalSorted) {
fout << node << " ";
}
return 0;
}