Pagini recente » Cod sursa (job #863351) | Cod sursa (job #2312316) | Cod sursa (job #1995441) | Cod sursa (job #1429749) | Cod sursa (job #2343174)
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <queue>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
vector <int> G[100005];
vector <int>::iterator it;
stack <int> st;
queue <int> q;
int viz[100005];
void eulerian(int node)
{
int other;
while(!G[node].empty())
{
st.push(node);
other=G[node].back();
G[node].pop_back();
G[other].erase(find(G[other].begin(),G[other].end(),node));
node=other;
}
}
void bfs(int u)
{
int v;
queue <int> q;
viz[u]=1;
q.push(u);
while(!q.empty())
{
u=q.front();
q.pop();
for(it=G[u].begin();it!=G[u].end();it++)
{
v=*it;
if(!viz[v])
{
viz[v]=1;
q.push(v);
}
}
}
}
int main()
{
int n,m,i,j,x,y,node;
bool conex=1,nrpare=1;
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
bfs(1);
for(i=1;i<=n;i++)
{
if(viz[i]==0)
{
conex=0;
break;
}
if(G[i].size()%2==1)
{
nrpare=0;
break;
}
}
if(nrpare==0 ||conex==0)
{
fout<<"-1\n";
return 0;
}
node=1;
do
{
eulerian(node);
node=st.top();
fout<<node<<" ";
st.pop();
}while(!st.empty());
return 0;
}