Pagini recente » Cod sursa (job #1109172) | Cod sursa (job #809753) | Cod sursa (job #845125) | Cod sursa (job #559241) | Cod sursa (job #1857452)
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
vector <int> ls[100005];
int n, m, x, y, i, l;
bool viz[100005];
int stiva[120005], vf;
void bf(int x) {
int i;
queue<int>c1;
c1.push(1);
while (c1.empty() == 0) {
x = c1.front();
l = ls[x].size();
c1.pop();
for (i = 0; i < l; i++) {
y = ls[x][i];
if (viz[y] == 0) {
viz[y] = 1;
c1.push(y);
}
}
}
}
int main() {
f >> n >> m;
while (m--) {
f >> x >> y;
ls[x].push_back(y);
ls[y].push_back(x);
}
bf(1);
for (i = 1; i <= n; i++)
if (ls[i].size()%2 == 1 || viz[i] == 0) {
g << -1;
return 0;
}
stiva[(vf=1)] = 1;
while (vf>0) {
x = stiva[vf], l = ls[x].size();
if (l == 0) {
g << x << ' ';
vf--;
continue;
}
y = ls[x][l-1];
stiva[++vf] = y;
ls[x].pop_back();
ls[y].erase(find(ls[y].begin(),ls[y].end(), x));
}
return 0;
}