Pagini recente » Rating Radu Dolea (LuxOcculta) | Cod sursa (job #1433856) | Istoria paginii utilizator/anghel_cipriana | Monitorul de evaluare | Cod sursa (job #2022418)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
/* ------ GRAPH DATA ------ */
vector <vector <int>> v;
int n, m;
stack<int> stk;
vector<bool> seen;
void readGraph() {
fin >> n >> m;
v = vector <vector <int>> (n + 1);
seen = vector <bool> (n + 1);
int x, y;
while (m--) {
fin >> x >> y;
v[x].push_back(y);
}
}
void dfs(int node) {
seen[node] = 1;
for (int newNode: v[node]) {
if (seen[newNode])
continue;
dfs(newNode);
}
stk.push(node);
}
void popStack() {
while (stk.size()) {
fout << stk.top() << ' ';
stk.pop();
}
}
int main()
{
readGraph();
dfs(1);
popStack();
return 0;
}