Pagini recente » Profil rodica_toma | Cod sursa (job #2893505) | Cod sursa (job #295086) | Cod sursa (job #735366) | Cod sursa (job #2374158)
#include <bits/stdc++.h>
using namespace std;
const int nmax=100005;
struct numere
{
int nod;
int poz;
};
int grad[nmax];
bool vis[5*nmax];
deque <int> rasp;
stack <int> st;
vector <numere> g[nmax];
void dfs(int start_node)
{
st.push(start_node);
for(int i=0;i<g[start_node].size();i++)
{
if(vis[g[start_node][i].poz]==false)
{
grad[start_node]--;
grad[g[start_node][i].nod]--;
vis[g[start_node][i].poz]=true;
dfs(g[start_node][i].nod);
}
}
while(!st.empty())
{
if(grad[st.top()]!=0)
break;
rasp.push_back(st.top());
st.pop();
}
}
bool corect(int n)
{
for(int i=1;i<=n;i++)
if(grad[i]%2==1)
return false;
return true;
}
int main()
{
freopen("ciclueuler.in", "r", stdin);
freopen("ciclueuler.out", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
for(int i=1;i<=m;i++)
{
int x, y;
scanf("%d%d", &x, &y);
grad[x]++;
grad[y]++;
numere k;
k.nod=x;
k.poz=i;
g[y].push_back(k);
k.nod=y;
g[x].push_back(k);
}
if(corect(n)==false)
{
printf("-1");
return 0;
}
dfs(1);
rasp.pop_front();
while(rasp.size())
{
printf("%d ", rasp.back());
rasp.pop_back();
}
return 0;
}