Pagini recente » Cod sursa (job #1600511) | Cod sursa (job #142138) | Cod sursa (job #228559) | Cod sursa (job #2232524) | Cod sursa (job #1425810)
#include <fstream>
#include <vector>
#include <list>
#include <stack>
#define N_Max 100010
#define M_Max 500010
using namespace std;
ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");
int N, M, grad_impar, neconex, Sol[M_Max];
list < int > L[N_Max];
stack < int > S;
void Delete_From_List(int i, int nod)
{
for (list < int > :: iterator it = L[i].begin(); it != L[i].end(); it++) {
if (*it == nod) {
L[i].erase(it);
return;
}
}
}
int main()
{
fin >> N >> M;
for (int x, y, i = 1; i <= M; i++) {
fin >> x >> y;
L[x].push_back(y);
L[y].push_back(x);
}
for (int i = 1; i <= N; i++) {
if (L[i].size() & 1) {
grad_impar = 1;
break;
}
}
S.push(1);
while (!S.empty())
{
int nod = S.top();
if (L[nod].size())
{
S.push(L[nod].front());
Delete_From_List(L[nod].front(), nod);
L[nod].erase(L[nod].begin());
}
else
{
Sol[++Sol[0]] = nod;
S.pop();
}
}
if (Sol[0] - 1 != M) neconex = 1;
if (grad_impar || neconex) {
fout << "-1\n";
}
else
{
for (int i = Sol[0]; i >= 2; i--) {
fout << Sol[i] << ' ';
}
fout << '\n';
}
fout.close();
return 0;
}