Pagini recente » Cod sursa (job #1893354) | Cod sursa (job #791429) | Cod sursa (job #1094283) | Cod sursa (job #2205960) | Cod sursa (job #2278622)
#include <fstream>
#include <vector>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
const int Nmax = 100005;
list<int> A[Nmax];
bool euler (int n)
{
for (int i = 1; i <= n; i++)
if (A[i].size() % 2)
return false;
return true;
}
void solve (int nod, stack<int>&stk)
{
while (A[nod].size())
{
int nod_vecin = A[nod].back();
A[nod].pop_back();
A[nod_vecin].erase(find(A[nod_vecin].begin(), A[nod_vecin].end(), nod));
solve(nod_vecin, stk);
}
stk.push(nod);
}
int main()
{
int n, m;
in >> n >> m;
stack<int> stk;
for (int i = 1; i <= m; i++)
{
int x, y;
in >> x >> y;
A[x].push_back(y);
A[y].push_back(x);
}
if (!euler(n))
{
out << -1;
return 0;
}
solve(1, stk);
while (!stk.empty())
{
out << stk.top() << ' ';
stk.pop();
}
return 0;
}