Pagini recente » Cod sursa (job #820742) | Cod sursa (job #2051667) | Cod sursa (job #1217617) | Cod sursa (job #1008350) | Cod sursa (job #1979324)
#include <cstdio>
#include <algorithm>
#include <stack>
#include <vector>
using namespace std;
const int NMAX = 100000;
int N, M, node;
vector<int>G[NMAX + 5];
stack<int>st;
void euler(int node){
while (G[node].size()){
st.push(node);
int other = G[node].back();
G[node].pop_back();
G[other].erase(find(G[other].begin(), G[other].end(), node));
node = other;
}
}
int main(){
freopen("ciclueuler.in", "r", stdin);
freopen("ciclueuler.out", "w", stdout);
scanf("%d%d", &N, &M);
int u, v;
while (scanf("%d%d", &u, &v) != EOF){
G[u].push_back(v);
G[v].push_back(u);
M++;
}
bool ok = true;
for (int i = 1; i <= N; ++i)
if (G[i].size() & 1){
ok = false;
break;
}
if (ok == false){
printf("-1\n");
return 0;
}
node = 1;
printf("%d ", node);
do{
euler(node);
node = st.top();
st.pop();
if (!st.empty())
printf("%d ", node);
}while (!st.empty());
return 0;
}