Pagini recente » Cod sursa (job #491305) | Cod sursa (job #1432192) | Cod sursa (job #1910433) | Cod sursa (job #2892453) | Cod sursa (job #2238945)
#include <fstream>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
vector<int>G[100005];
vector<int>::iterator it;
stack<int>st;
int n,m,i,j,k,l,r,x,y,s,smax,viz[100005];
bool ok;
void dfs(int x)
{
int q;
viz[x]=1;
for(int v=0;v<G[x].size();v++)
{
q=G[x][v];
if(!viz[q])
{
dfs(q);
}
}
}
void euler(int x)
{
int node,other;
node=x;
while(G[node].size()>0)
{
other=G[node][0];
// it=find(G[other].begin(),G[other].end(),node);
G[other].erase(find(G[other].begin(),G[other].end(),node));
G[node].erase(G[node].begin());
euler(other);
}
st.push(node);
}
int main()
{
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
for(i=1;i<=n;i++)
if(G[i].size()%2==1)
ok=1;
if(!ok)
{
dfs(1);
for(i=1;i<=n;i++)
if(!viz[i])
ok=1;
}
if(!ok)
{
//st.push(1);
euler(1);
// fout<<1<<" " ;
st.top();
while(st.size()>1)
{
fout<<st.top()<<" ";
st.pop();
/*
if(G[st.top()].size()>0)
euler(st.top());*/
}
}
else
fout<<-1;
// fout<<st.size();
return 0;
}