Pagini recente » Cod sursa (job #2009904) | Cod sursa (job #2674844) | Cod sursa (job #2596051) | Cod sursa (job #1009933) | Cod sursa (job #1830366)
#include <fstream>
#include <stack>
#include <algorithm>
#include <vector>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n, m, i, j, x, y, l;
bool viz[100005];
vector<int> ls[100005];
int dim[100005];
inline void df(int x) {
int i;
viz[x] = 1;
for (i = 0; i < ls[x].size(); i++)
if (viz[ls[x][i]] == 0)
df(ls[x][i]);
}
int main() {
f >> n >> m;
while (m--) {
f >> x >> y;
ls[x].push_back(y);
ls[y].push_back(x);
dim[x]++, dim[y]++;
}
df(1);
for (i = 1; i <= n; i++)
if (dim[i]%
2 == 1 || viz[i] == 0) {
g << -1;
return 0;
}
stack <int> stiva;
stiva.push(1);
while (stiva.empty() == 0) {
x = stiva.top(), l = ls[x].size();
if (l == 0) {
g << stiva.top() <<' ';
stiva.pop();
continue;
}
y = ls[x][l-1];
stiva.push(y);
ls[x].pop_back();
ls[y].erase(find(ls[y].begin(), ls[y].end(), x));
}
return 0;
}