Pagini recente » Cod sursa (job #289376) | Cod sursa (job #1634637) | Cod sursa (job #6921) | Cod sursa (job #3183357) | Cod sursa (job #2119189)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
const int M = 5e5 + 5;
int deg[N];
vector <int> edge[N];
bool viz[N];
bool used[M];
int frm[M], to[M];
vector <int> ans;
void dfs(int x){
viz[x] = true;
for(auto &it : edge[x]){
if(viz[it] == false){
dfs(it);
}
}
}
int main(){
// ios_base::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m;
fin >> n >> m;
for(int i = 1;i <= m;i++){
int x, y;
fin >> x >> y;
edge[x].push_back(i);
edge[y].push_back(i);
frm[i] = x;
to[i] = y;
deg[x]++;
deg[y]++;
}
dfs(1);
for(int i = 1;i <= n;i++){
if(viz[i] == 0 || deg[i]%2 == 1){
fout << -1;
return 0;
}
}
stack <int> s;
s.push(1);
while(s.empty() == false){
int x = s.top();
if(edge[x].empty() == false){
int e = edge[x].back();
edge[x].pop_back();
if(used[e] == false){
used[e] = true;
int y = frm[e] ^ to[e] ^ x;
s.push(y);
}
}else{
s.pop();
ans.push_back(x);
}
}
for(auto &it : ans){
fout << it << ' ';
}
return 0;
}