Pagini recente » Cod sursa (job #2641143) | Cod sursa (job #1488791) | Statistici Miruna Ruxandra Budoias (MirunaBudoias) | Monitorul de evaluare | Cod sursa (job #2123809)
#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#define NMAX 100005
using namespace std;
int N, M;
bool viz[5*NMAX];
vector <pair <int, int> > G[NMAX];
vector <pair <int, int> >::iterator it;
stack <int> s;
void read()
{
scanf("%d %d", &N, &M);
for(int i=1; i<=M; ++i)
{
int x, y;
scanf("%d %d", &x, &y);
G[x].push_back(make_pair(y, i));
G[y].push_back(make_pair(x, i));
}
}
bool checkEuler()
{
for(int i=1; i<=N; ++i)
if(G[i].size() % 2)
return false;
return true;
}
void euler()
{
s.push(1);
int nod, indiceMuchie, vecin;
while(!s.empty())
{
nod = s.top();
if(!G[nod].empty())
{
indiceMuchie = G[nod].back().second;
vecin = G[nod].back().first;
G[nod].pop_back();
if(viz[indiceMuchie] == false)
{
viz[indiceMuchie] = true;
s.push(vecin);
}
}
else
{
s.pop();
printf("%d ", nod);
}
}
}
int main()
{
freopen("ciclueuler.in", "r", stdin);
freopen("ciclueuler.out", "w", stdout);
read();
if(checkEuler())
euler();
else
printf("-1");
return 0;
}