Pagini recente » Cod sursa (job #572708) | Cod sursa (job #373636) | Cod sursa (job #1474270) | Cod sursa (job #3252222) | Cod sursa (job #2185685)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int nmax= 100000;
const int mmax= 500000;
int n, m;
bool u[mmax+1];
int a[mmax+1], b[mmax+1];
vector <int> g[nmax+1], s;
int main( ) {
fin>>n>>m;
for ( int i= 1, x, y; i<=m; ++i ) {
fin>>x>>y;
g[x].push_back(i);
g[y].push_back(i);
a[i]= x, b[i]= y;
}
for ( int i= 1; i<=n; ++i ) {
if ( (int)g[i].size()%2==1 ) {
fout<<"-1\n";
return 0;
}
}
for ( s.push_back(1); !s.empty(); ) {
int x= s.back();
if ( (int)g[x].size()>0 ) {
int y= g[x].back();
g[x].pop_back();
if ( u[y]==0 ) {
u[y]= 1;
if ( b[y]==x ) {
s.push_back(a[y]);
} else {
s.push_back(b[y]);
}
}
} else {
s.pop_back();
if ( (int)s.size()>0 ) {
fout<<x<<" ";
}
}
}
fout<<"\n";
return 0;
}