Pagini recente » Cod sursa (job #2590711) | Cod sursa (job #67493) | Cod sursa (job #872472) | Cod sursa (job #1983416) | Cod sursa (job #1759227)
#include <fstream>
#include <list>
#include <queue>
using namespace std;
ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");
int n, m, S[500010], Sol[500010];
bool F[100010];
queue < int > Q;
list < int > L[100010];
void BFS(int nod)
{
F[nod] = true;
Q.push(nod);
while (!Q.empty())
{
nod = Q.front();
Q.pop();
for (list < int > :: iterator it = L[nod].begin(); it != L[nod].end(); it ++)
{
if (!F[*it])
{
F[*it] = true;
Q.push(*it);
}
}
}
}
bool Verif_Euler()
{
BFS(1);
for (int i = 1; i <= n; i ++)
{
if (L[i].size() & 1 || F[i] == false)
{
return 0;
}
}
return 1;
}
void Delet(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;
}
}
}
void Euler()
{
S[++S[0]] = 1;
while (S[0])
{
int nod = S[S[0]];
if (!L[nod].empty())
{
S[++S[0]] = L[nod].front();
Delet(L[nod].front(), nod);
L[nod].pop_front();
}
else
{
Sol[++Sol[0]] = nod;
S[0] --;
}
}
}
void Write_Lant_Euler(bool aux)
{
if (aux)
{
Euler();
for (int i = 1; i < Sol[0]; i ++)
{
fout << Sol[i] << ' ';
}
}
else
{
fout << "-1";
}
fout << '\n';
fout.close();
}
int main()
{
fin >> n >> m;
for (int i = 1, x, y; i <= m; i ++)
{
fin >> x >> y;
L[x].push_back(y);
L[y].push_back(x);
}
Write_Lant_Euler(Verif_Euler());
return 0;
}