Pagini recente » Cod sursa (job #698142) | Cod sursa (job #517631) | Cod sursa (job #331619) | Cod sursa (job #2113668) | Cod sursa (job #632767)
Cod sursa(job #632767)
#include <iostream>
#include <stdio.h>
#include <stack>
#include <queue>
#include <list>
#define LgN 100010
using namespace std;
list <int> G[LgN];
list <int> L;
stack <int> S;
queue <int> Q;
int viz[LgN], deg[LgN];
int n,m;
void citire()
{
freopen("ciclueuler.in","r",stdin);
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d %d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
deg[x]++, deg[y]++;
}
}
//void bfs(int v)
//{
// Q.push(v);
// viz[v]=1;
//
// while( !Q.empty())
// {
// v = Q.front();
// Q.pop();
// for( typeof(G[v].begin()) it = G[v].begin() ; it != G[v].end() ; it++ )
// if(viz[*it]==0)
// {
// viz[*it]=1;
// Q.push(*it);
// }
// }
//}
int conex(int nod)
{
for( typeof(G[nod].begin()) it = G[nod].begin() ; it != G[nod].end() ; it++ )
if(viz[*it]==0)
{
viz[*it] = 1;
conex(*it);
}
//bfs(1);
for( int i=1;i<=n;i++ )
if(viz[i] == 0)
return 0;
return 1;
}
int verif()
{
for(int i=1;i<=n;i++)
if(deg[i]%2==1)
return 0;
if( ! conex(1) )
return 0;
return 1;
}
void sterg(int x, int y)
{
G[x].pop_front();
deg[x]--;
deg[y]--;
for( typeof(G[y].begin()) it = G[y].begin() ; it != G[y].end() ; it++ )
if( *it == x )
{
G[y].erase(it);
break;
}
}
void eulerian( int nod )
{
while( ! G[nod].empty() )
{
int x = G[nod].front();
S.push(nod);
sterg(nod, x);
nod = x;
}
}
void af_sol()
{
int x = 1;
if( !verif() )
printf("-1");
else
{
do
{
eulerian(x);
int x = S.top();
S.pop();
L.push_front(x);
} while( !S.empty() );
for( typeof(L.begin()) it = L.begin() ; it != L.end() ; it++ )
printf("%d " ,*it);
}
}
int main()
{
freopen("ciclueuler.out","w",stdout);
citire();
af_sol();
return 0;
}