Pagini recente » Cod sursa (job #2909925) | Cod sursa (job #2292154) | Cod sursa (job #1244241) | Cod sursa (job #2805897) | Cod sursa (job #1623877)
#include <fstream>
#include <algorithm>
#include <stack>
#include <vector>
#include <iostream>
#include <bitset>
#define DIM 100005
using namespace std;
ifstream fin("euler.in");
ofstream fout("euler.out");
int N,M;
stack <int> S;
vector <int> L[DIM];
bitset <DIM> viz;
int check1(){
for(int i=1;i<=N;i++)
if(L[i].size()%2!=0)
return 0;
return 1;
}
void dfs(int x){
viz[x]=1;
for(int i=0;i<L[x].size();i++)
if(!viz[L[x][i]])
dfs(L[x][i]);
}
int check2(){
dfs(1);
for(int i=2;i<=N;i++)
if(!viz[i])
return 0;
return 1;
}
int main(){
fin >> N >> M;
for(int i=1;i<=M;i++){
int x,y;
fin >> x >> y;
L[x].push_back(y);
L[y].push_back(x);
}
if(!(check1() && check2())){
fout << "-1\n";
}
S.push(1);
while(!S.empty()){
int nod=S.top();
if(!L[nod].size()){
fout << nod << " ";
S.pop();
}
else{
int x=L[nod].back();
S.push(x);
L[nod].pop_back();
L[x].erase(find(L[x].begin(),L[x].end(),nod));
}
}
return 0;
}