Pagini recente » Cod sursa (job #43449) | Cod sursa (job #2782625) | Cod sursa (job #702231) | Cod sursa (job #2220023) | Cod sursa (job #1894103)
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;
const int nmx = 100002;
const int mmx = 500002;
struct muchie
{
int nod1,nod2;
bool sters = false;
} v[mmx];
int n,m;
int grad[nmx];
vector <int> g[nmx];
stack <int> st;
void citire()
{
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; ++i)
{
scanf("%d %d", &v[i].nod1, &v[i].nod2);
g[v[i].nod1].push_back(i);
g[v[i].nod2].push_back(i);
++ grad[v[i].nod1];
++ grad[v[i].nod2];
}
}
bool nu_admite()
{
for(int i = 1; i <= n; ++i)
if(grad[i] % 2)
return 1;
return 0;
}
void euler()
{
st.push(1);
while(not st.empty())
{
int nod = st.top();
while(g[nod].size() && v[g[nod].back()].sters == true)
g[nod].pop_back();
if(g[nod].size())
{
int next = g[nod].back();
v[next].sters = true;
int next_nod = (nod == v[next].nod1 ? v[next].nod2 : v[next].nod1);
st.push(next_nod);
}
else
{
printf("%d ", st.top());
st.pop();
}
}
}
int main()
{
freopen("ciclueuler.in", "r", stdin);
freopen("ciclueuler.out", "w", stdout);
citire();
if(nu_admite())
{
printf("-1\n");
return 0;
}
euler();
return 0;
}