Pagini recente » Cod sursa (job #163533) | Cod sursa (job #28168) | Cod sursa (job #2631498) | Cod sursa (job #1821569) | Cod sursa (job #1248596)
#include <fstream>
#include <list>
#include <queue>
#include <stack>
#define DIM 100001
using namespace std;
ofstream out("ciclueuler.out");
stack <int> st;
queue <int> q;
list <int> nod[DIM];
short int vis[DIM];
int v, vf,a , b, n, mu, y, x;
void sterge()
{
for( list <int>::iterator i=nod[vf].begin(); i!=nod[vf].end(); ++i)
{
if(*i==v)
{
nod[vf].erase(i);
break;
}
}
}
void ciclu()
{
st.push(1);
while(!st.empty())
{
v=st.top();
if(!nod[v].empty())
{
vf=nod[v].front();
st.push(vf);
nod[v].pop_front();
sterge();
continue;
}
st.pop();
out<<v<<" ";
}
}
int verif()
{
q.push(1);
vis[1]=1;
while(!q.empty())
{
vf=q.front();
for( list <int>::iterator i=nod[vf].begin(); i!=nod[vf].end(); ++i)
{
if(vis[*i]==0)
{
q.push(*i);
vis[*i]=1;
}
}
q.pop();
}
y=1;
for(int i=1; i<=n; ++i)
{
if(vis[i]==0||nod[i].size()%2==1)
{
y=0;
break;
}
}
return y;
}
int main()
{
ifstream in("ciclueuler.in");
in>>n>>mu;
for(int i=1; i<=mu; ++i)
{
in>>a>>b;
nod[a].push_back(b);
nod[b].push_back(a);
}
x=verif();
if(x==1)
ciclu();
else out<<"-1\n";
in.close();
out.close();
return 0;
}