Pagini recente » Cod sursa (job #179437) | Cod sursa (job #332178) | Cod sursa (job #717150) | Cod sursa (job #2714271) | Cod sursa (job #1635520)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int NMAX = 100005;
vector < int > SOL,S;
list < int > G[NMAX];
int V[NMAX];
list < int > ::iterator it;
int main(){
int n,m,x,y,nod,go;
fin >> n >> m;
for(int i = 1; i <=m; i++){
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
V[x] ++; V[y] ++;
}
for(int i = 1; i <= n; i++){
if(V[i] % 2 != 0){
fout << -1;
return 0;
}
}
S.push_back(1);
while(!S.empty()){
nod = S.back(); S.pop_back();
while(!G[nod].empty()){
go = G[nod].front();
for( it = G[go].begin(); it != G[go].end(); it++){
if(*it == nod){
G[go].erase(it);
break;
}
}
S.push_back(nod);
G[nod].pop_front();
nod = go;
}
SOL.push_back(nod);
}
for(int i = 0; i < SOL.size(); i++)
fout << SOL[i] << " ";
return 0;
}