Pagini recente » Cod sursa (job #3242146) | Cod sursa (job #692294) | Cod sursa (job #2608206) | Cod sursa (job #3242104) | Cod sursa (job #1023167)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
vector<vector<int>> graph;
void dfs(int node, vector<bool> &visited) {
visited[node] = true;
for (int i = 0; i < graph[node].size(); i++) {
int v = graph[node][i];
if (!visited[v]) {
dfs(v, visited);
}
}
}
bool isConnected() {
vector<bool> visited;
visited.resize(graph.size(), false);
dfs(1, visited);
for (int i = 1; i < visited.size(); i++) {
if (!visited[i]) {
return false;
}
}
return true;
}
void removeEdge(int x, int y) {
for (int i = 0; i < graph[x].size(); i++) {
if (graph[x][i] == y) {
graph[x][i] = graph[x][graph[x].size()-1];
graph[x].pop_back();
break;
}
}
}
void computeCycle(vector<int> &cycle) {
stack<int> returnNode;
int currentNode = 1;
returnNode.push(currentNode);
while (!returnNode.empty()) {
currentNode = returnNode.top();
if (graph[currentNode].size() == 0) {
returnNode.pop();
cycle.push_back(currentNode);
} else {
int nextNode = graph[currentNode][graph[currentNode].size()-1];
graph[currentNode].pop_back();
removeEdge(nextNode, currentNode);
returnNode.push(nextNode);
}
}
}
int main() {
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n, m, x, y;
f >> n >> m;
graph.resize(n+1);
for (int i = 0; i < m; i++) {
f >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
bool isEuler = true;
for (int i = 1; i <= n; i++) {
if (graph[i].size() % 2 == 1) {
isEuler = false;
break;
}
}
if (isEuler && !isConnected()) {
isEuler = false;
}
if (!isEuler) {
cout << "-1" << endl;
} else {
vector<int> eulerCycle;
computeCycle(eulerCycle);
for (int i = 0; i < eulerCycle.size()-1; i++) {
g << eulerCycle[i] << " ";
}
}
return 0;
}