Pagini recente » Cod sursa (job #607655) | Cod sursa (job #169200) | Cod sursa (job #837398) | Cod sursa (job #576816) | Cod sursa (job #2528231)
#include <fstream>
//#include <iostream>
//#include<stdio.h>
#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 node)
{
while(!G[node].empty())
{
st.push(node);
int other=G[node].back();
// it=find(G[other].begin(),G[other].end(),node);
G[other].erase(find(G[other].begin(),G[other].end(),node));
G[node].pop_back();
node=other;
}
}
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(int((G[i].size())&1))
ok=1;
if(!ok)
{
l=1;
fout<<1<<" ";
do
{
euler(l);
l=st.top();
fout<<l<<" ";
st.pop();
}while(st.size()>1);
}
else
fout<<-1<<"\n";
return 0;
}