Pagini recente » Cod sursa (job #2215062) | Cod sursa (job #1278526) | Cod sursa (job #2572438) | Cod sursa (job #263578) | Cod sursa (job #3217472)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int MAX_SIZE = 100000;
vector<int> graph[MAX_SIZE + 1];
vector<int> answer;
void deleteEdge(int node1, int node2) {
for (vector<int>::iterator it = graph[node1].begin(); it < graph[node1].end(); ++it) {
if (*it == node2) {
graph[node1].erase(it);
return;
}
}
}
void euler(int currentNode) {
while (graph[currentNode].empty() == 0) {
int nextNode = *graph[currentNode].begin();
deleteEdge(currentNode, nextNode);
deleteEdge(nextNode, currentNode);
euler(nextNode);
}
answer.push_back(currentNode);
}
int main() {
int noNodes, noEdges;
fin >> noNodes >> noEdges;
for (int i = 1; i <= noEdges; ++i) {
int startNode, endNode;
fin >> startNode >> endNode;
graph[startNode].push_back(endNode);
graph[endNode].push_back(startNode);
}
euler(1);
for (int i = 0; i < answer.size(); ++i) {
fout << answer[i] << ' ';
}
return 0;
}