Pagini recente » Cod sursa (job #1753578) | Cod sursa (job #1384909) | Istoria paginii runda/concurs_2 | Cod sursa (job #2589434) | Cod sursa (job #1889774)
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
vector<int>ls[100005],sol;
bool viz[100005],ok;
int n, m,i,j,x,y;
stack<int>stiva;
void df(int x) {
int i, l = ls[x].size(), y;
viz[x] = 1;
for (i = 0; i < l; i++) {
y = ls[x][i];
if (viz[y] == 0)
df(y);
}
//g << 1;
}
int main() {
f >> n >> m;
while (m--) {
f >> x >> y;
ls[x].push_back(y);
ls[y].push_back(x);
}
df(1);
for (i = 1; i<= n; i++)
if (viz[i] == 0 || ls[i].size()%2==1) {
g << -1;
return 0;
}
stiva.push(1);
ok = 1;
while (stiva.empty()==0) {
x = stiva.top();
while (ls[x].size() > 0) {
y = ls[x].front();
stiva.push(y);
ls[x].erase(ls[x].begin());
ls[y].erase(find(ls[y].begin(), ls[y].end(), x));
x = y;
//g<<1;
}
//g<<'\n';
sol.push_back(stiva.top());
stiva.pop();
}
for (auto &it:sol)
g << it<< " ";
return 0;
}