Pagini recente » Cod sursa (job #2517113) | Cod sursa (job #688965) | Cod sursa (job #1836653) | Cod sursa (job #2659710) | Cod sursa (job #2371394)
#include <cstdio>
#include <list>
#include <vector>
#define N 100050
using namespace std;
vector <int> g[N];
list <int> q;
int n,m,i,x,y,nr;
bool ok;
void euler(int x)
{
q.push_back(x);
vector <int>::iterator it;
while ( !q.empty() )
{
x=q.front ();
if(g[x].empty())
{
q.pop_front();
if (!q.empty()) printf("%d ", x);
}
else
{
int nod=g[x].back();
q.push_front(nod);
g[x].pop_back();
for ( it = g[nod].begin(); it!=g[nod].end(); it++)
if (*it==x)
{
g[nod].erase(it);
break;
}
}
}
}
int main()
{
freopen ("ciclueuler.in", "r",stdin );
freopen ("ciclueuler.out", "w", stdout);
scanf ("%d%d", &n,&m);
for (i=1; i<=m; i++)
{
scanf ("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
ok=true;
for (i=1; i<=n; i++)
if (g[i].size()%2==1)
{
ok=false;
break;
}
if (!ok)
printf ("-1\n");
else
euler (1);
return 0;
}