Pagini recente » Cod sursa (job #1976547) | Cod sursa (job #1468747) | Cod sursa (job #1271177) | Cod sursa (job #1680707) | Cod sursa (job #3040445)
#include<fstream>
#include<vector>
#include<stack>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int const N = 5e5 + 3;
int n , m;
int st[N] , dr[N] , viz[N];
vector<int> v[N];
stack<int> stk;
int main(){
fin >> n >> m;
for(int i = 1 ; i <= m ; ++ i){
fin >> st[i] >> dr[i];
v[st[i]].push_back(i);
v[dr[i]].push_back(i);
}
for(int i = 1 ; i <= n ; ++ i){
if(v[i].size() & 1){
fout << "-1\n";
return 0;
}
}
stk.push(1);
while(!stk.empty()){
int x = stk.top();
if(v[x].empty()){
stk.pop();
if(!stk.empty())
fout << x << ' ';
}else{
int e = v[x].back();
v[x].pop_back();
if(viz[e])
continue;
viz[e] = 1;
int y = st[e] ^ dr[e] ^ x;
stk.push(y);
}
}
return 0;
}