Pagini recente » Cod sursa (job #1884411) | Cod sursa (job #527735) | Cod sursa (job #48250) | Cod sursa (job #864690) | Cod sursa (job #1022803)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;
int n,m;
vector<int> adjacencyList[100001];
int solutionNr = 0;
int solution [500004];
int st[500001];
void dfs(int startNode){
int stPointer = 0;
st[stPointer] = startNode;
while (stPointer >= 0){
int currentNode = st[stPointer];
if (adjacencyList[currentNode].size() > 0){
int nextNode = adjacencyList[currentNode].back();
st[++stPointer] = nextNode;
adjacencyList[currentNode].pop_back();
adjacencyList[nextNode].erase(find(adjacencyList[nextNode].begin(), adjacencyList[nextNode].end(), currentNode));
} else {
stPointer--;
solution[solutionNr++] = currentNode;
}
}
}
int main()
{
freopen("ciclueuler.in", "r", stdin);
freopen("ciclueuler.out", "w", stdout);
scanf("%d %d", &n, &m);
int node1, node2;
for (int i=0; i<m; i++){
scanf("%d %d", &node1, &node2);
adjacencyList[node1].push_back(node2);
adjacencyList[node2].push_back(node1);
}
dfs(1);
for (int i=0; i<solutionNr-1; i++){
printf("%d ", solution[i]);
}
return 0;
}