Pagini recente » Cod sursa (job #1726307) | Cod sursa (job #1540211) | Cod sursa (job #191389) | Cod sursa (job #2068221) | Cod sursa (job #2328547)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define N 100003
#define M 500004
using namespace std;
int usedEdge[M],from[M],to[M],n,m;
vector<int> G[N],sol;
int dfs();
int main() {
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
fin >> n >> m;
int x, y;
for (int i = 1; i <= m; i++) {
fin >> x >> y;
from[i] = x; to[i] = y;
G[x].push_back(i); G[y].push_back(i);
}
for (int i = 1; i <= n; i++) {
if (G[i].size() % 2 != 0) {
fout << "-1";
return 0;
}
}
dfs();
for (int i = 0; i < sol.size(); i++) {
fout << sol[i] << " ";
}
}
int dfs() {
vector<int> nodesStack;
nodesStack.push_back(1);
while (!nodesStack.empty()) {
int frontNode = nodesStack.back();
//cout << "front" << frontNode <<endl;
if (!G[frontNode].empty()) {
int edge = G[frontNode].back();
G[frontNode].pop_back();
if (usedEdge[edge] != 1) {
usedEdge[edge] = 1;
int nodePushed = from[edge] != frontNode ? from[edge] : to[edge];
nodesStack.push_back(nodePushed);
}
} else {
//cout<< "rest" << frontNode<<endl;
sol.push_back(frontNode);
nodesStack.pop_back();
}
}
}