Pagini recente » Cod sursa (job #547735) | Cod sursa (job #472647) | Cod sursa (job #1328591) | Cod sursa (job #1259759) | Cod sursa (job #1758106)
#include <fstream>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;
typedef vector<list<int>> Graph;
void delete_edge(Graph& graph, int a, int b) {
graph[a].erase(find(graph[a].begin(), graph[a].end(), b));
graph[b].erase(find(graph[b].begin(), graph[b].end(), a));
}
void euler(Graph& graph, vector<int>& ans, int node) {
while (!graph[node].empty()) {
int adj = *graph[node].begin();
delete_edge(graph, node, adj);
euler(graph, ans, adj);
}
ans.push_back(node);
}
int main() {
Graph graph;
vector<int> ans;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m;
fin >> n >> m;
graph.resize(n + 1);
for (int e = 0; e < m; ++e) {
int x, y;
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
euler(graph, ans, 1);
if (n > 1) {
ans.erase(ans.end() - 1);
}
for (int elem : ans) {
fout << elem << " ";
}
fout << "\n";
return 0;
}