Pagini recente » Cod sursa (job #2428778) | Cod sursa (job #11003) | Cod sursa (job #2337460) | Cod sursa (job #657020) | Cod sursa (job #1438919)
#include <iostream>
#include <fstream>
#include <list>
#include <deque>
#define NMAX 100001
int n, m;
std::list<int>graf[NMAX];
std::deque<int> coada;
std::list<int>::iterator it;
void citire()
{
int x, y;
std::ifstream f("ciclueuler.in");
f >> n >> m;
for (int i = 1; i <= m;i++)
{
f >> x >> y;
graf[x].push_back(y);
graf[y].push_back(x);
}
}
int main()
{
citire();
std::ofstream g("ciclueuler.out");
bool ok = true;
int x, y;
for (int i = 1; i <= n; i++)
{
if (graf[i].size() % 2 == 0)
{
ok = false;
break;
}
}
if (ok != false)
{
g << "-1\n";
return 0;
}
coada.push_front(1);
while (!coada.empty())
{
x = coada.front();
if (graf[x].size() == 0)
{
g << x << " ";
coada.pop_front();
}
else
{
y = graf[x].front();
graf[x].pop_front();
coada.push_front(y);
for (it = graf[y].begin(); it != graf[y].end(); it++)
{
if (*it == x)
{
graf[y].erase(it);
break;
}
}
}
}
return 0;
}