Pagini recente » Cod sursa (job #1294090) | Statistici Andra Mihaela Andruta (andra_2602) | Cod sursa (job #1979946) | Cod sursa (job #821574) | Cod sursa (job #1552843)
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <stack>
#define nmax 100010
using namespace std;
int n,m,x,y,rang[nmax];
vector <int> g[nmax],sol;
stack <int> st;
int main() {
freopen("ciclueuler.in","r",stdin);
freopen("ciclueuler.out","w",stdout);
scanf("%d %d",&n,&m);
for (int i=1;i<=m;i++) {
scanf("%d %d",&x,&y); g[x].push_back(y); g[y].push_back(x); rang[x]++; rang[y]++;
}
for (int i=1;i<=n;i++)
if (rang[i]%2==1) { puts("-1"); return 0; }
st.push(1);
while (!st.empty()) {
x=st.top();
if (g[x].size()==0) {
sol.push_back(x); st.pop(); continue;
}
int y=g[x][g[x].size()-1];
st.push(y); g[x].pop_back();
g[y].erase(find(g[y].begin(),g[y].end(),x));
}
for (int i=0;i<sol.size();i++) printf("%d ",sol[i]);
return 0;
}