Pagini recente » Cod sursa (job #960427) | Cod sursa (job #1196613) | Cod sursa (job #3197676) | Cod sursa (job #23536) | Cod sursa (job #2343149)
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
vector <int> G[100005];
stack <int> st;
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 dfs(int u)
{
int v,j;
viz[u]=1;
for(j=0;j<G[u].size();j++)
{
v=G[u][j];
if(viz[v]==0)
dfs(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);
}
dfs(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;
}