Pagini recente » Cod sursa (job #76862) | Cod sursa (job #198826) | Infoarena Monthly 2014, Clasament Runda 4 | Cod sursa (job #250505) | Cod sursa (job #3169556)
#include<fstream>
#include<vector>
#include<stack>
#define dim 100001
using namespace std;
ifstream in ("ciclueuler.in" );
ofstream out("ciclueuler.out");
struct muchie {
int x;
int y;
bool f;
};
muchie v[dim*5];
int grad[dim], ciclu[dim], n, m, cnt;
vector<int> G[dim];
stack<int> stiva;
int main() {
in >> n >> m;
for (int i = 1, x, y; i <= m; i++) {
in >> x >> y;
grad[x]++;
grad[y]++;
v[i].x = x;
v[i].y = y;
G[x].push_back(i);
G[y].push_back(i);
}
for (int i = 1; i <= n; i++)
if (grad[i] % 2 == 1) {
out << -1;
return 0;
}
stiva.push(1);
while(stiva.empty() == false) {
int nod = stiva.top();
int ok = 0;
while(G[nod].size()) {
int mch = G[nod].back();
G[nod].pop_back();
if (v[mch].f == 0) {
v[mch].f = 1;
ok = 1;
int x = v[mch].x,
y = v[mch].y;
if (x != nod)
stiva.push(x);
else
stiva.push(y);
break;
}
}
if (ok == 0) {
ciclu[++cnt] = nod;
stiva.pop();
}
}
if (cnt != m + 1) {
out << -1;
return 0;
}
for (int i = 1; i < cnt; i++)
out << ciclu[i] << " ";
}