Pagini recente » Cod sursa (job #2645247) | Cod sursa (job #1951301) | Cod sursa (job #1488003) | Cod sursa (job #1835398) | Cod sursa (job #1369212)
#include <fstream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
fstream f("ciclueuler.in", ios::in);
fstream g("ciclueuler.out",ios::out);
const int nmax = 100010;
vector <int> a[nmax];
queue <int> q;
stack <int> s;
int n, m, i, x, y, grad[nmax], sol[nmax], viz[nmax], imposibil, nrs, nc, v;
void citire()
{
f >> n >> m;
for (i = 1; i <= m; i++)
{
f >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
grad[x]++;
grad[y]++;
}
}
void verifica()
{
for (i = 1; i <= n; i++) if (grad[i] % 2 == 1) {
imposibil = 1;
return;
}
/*q.push(1);
viz[1] = 1;
while (!q.empty())
{
nc = q.front(); q.pop();
for (vector <int>::iterator it = a[nc].begin(); it != a[nc].end(); ++it) if (!viz[*it]) {
viz[*it] = 1;
q.push(*it);
}
}
for (i = 1; i <= n; i++) if (!viz[i]) {
imposibil = 1;
return;
}*/
}
void rezolva()
{
s.push(1);
while (!s.empty())
{
nc = s.top();
if (a[nc].size())
{
v = a[nc].back();
s.push(v);
a[nc].pop_back();
a[v].erase(find(a[v].begin(), a[v].end(), nc));
}
else
{
nrs++;
sol[nrs] = nc;
s.pop();
if (nrs == m) return;
}
}
}
int main()
{
citire();
verifica();
if (imposibil) g << "-1";
else
{
rezolva();
for (i = 1; i <= nrs; i++) g << sol[i] << " ";
}
return 0;
}