Pagini recente » Cod sursa (job #2648595) | Cod sursa (job #2739869) | Cod sursa (job #1477866) | Cod sursa (job #1675849) | Cod sursa (job #1236095)
#include <fstream>
#include <vector>
#include <stack>
#include <list>
using namespace std;
ifstream ka("ciclueuler.in");
ofstream ki("ciclueuler.out");
const int N_MAX = 100000;
const int M_MAX = 500000;
list <int> muchii[N_MAX + 1];
bool folosita[M_MAX + 1], vazut[N_MAX + 1];
int grad[N_MAX + 1];
stack <int> stiva;
vector <int> raspuns;
void sterge(int x, int y)
{
muchii[x].pop_front();
for(list<int>::iterator it = muchii[y].begin(); it != muchii[y].end(); ++it)
if(*it == x)
{
muchii[y].erase(it);
break;
}
}
void cautare(int c)
{
while(!muchii[c].empty())
{
stiva.push(c);
int d = muchii[c].front();
sterge(c, d);
c = d;
}
}
int n, m;
int main()
{
ka >> n >> m;
int x, y;
for(int i = 1; i <= m; i++)
{
ka >> x >> y;
muchii[x].push_back(y);
muchii[y].push_back(x);
grad[x]++;
grad[y]++;
}
stiva.push(x);
while(!stiva.empty())
{
int curent = stiva.top();
vazut[curent] = true;
stiva.pop();
cautare(curent);
raspuns.push_back(curent);
}
bool euler = true;
for(int i = 1; i <= n && euler; i++)
{
if(!vazut[i] || grad[i] % 2 != 0)
euler = false;
}
if(euler)
for(int i = 0; i < raspuns.size() - 1; i++)
ki << raspuns[i] << " ";
else
ki << -1;
}