Pagini recente » Cod sursa (job #2028294) | Cod sursa (job #2285142) | Cod sursa (job #304753) | Cod sursa (job #155932) | Cod sursa (job #1992628)
#include <fstream>
#include <vector>
using namespace std;
ofstream fout ("ciclueuler.out");
ifstream fin ("ciclueuler.in");
int n,m,i,a,b,grad[100005],st[100005],folosit[500005],ind[100005],nod,top,lung;
vector < pair < int , int > > w[100005];
pair < int , int > aux;
int main()
{
fin>>n>>m;
for( i = 1 ; i <= m ; i++ )
{
fin>>a>>b;
w[ a ].push_back( { b , i } );
w[ b ].push_back( { a , i } );
grad[ a ]++;
grad[ b ]++;
}
for( i = 1 ; i <= n ; i++ )
{
if( grad[ i ] == 0 || grad[ i ] % 2 )
{
fout<<-1;
return 0;
}
}
st[ 1 ] = 1;
top = 1;
while( top > 0 )
{
nod = st[ top ];
lung = w[ nod ].size();
while( ind[ nod ] < lung )
{
aux = w[ nod ][ ind[ nod ] ];
if( folosit[ aux.second ] == 0 )
{
folosit[ aux.second ] = 1;
st[ ++top ] = aux.first;
break;
}
ind[ nod ]++;
}
if( ind[ nod ] == lung )
{
if( top > 0 )
{
fout<<nod<<" ";
top--;
}
}
}
}