Pagini recente » Cod sursa (job #2624030) | Cod sursa (job #2710759) | Cod sursa (job #3198868) | Cod sursa (job #817407) | Cod sursa (job #2342259)
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
vector<int>G[100005];
vector<int>::iterator it;
stack<int>st;
void Euler(int node)
{
int other;
while(!G[node].empty())
{
st.push(node);
other=G[node].back();
G[node].pop_back();
it=find(G[other].begin(),G[other].end(),node);
G[other].erase(it);
node=other;
}
}
int main()
{
int n,x,y,i,m,now;
bool ok;
fin>>n>>m;
for(i=1;i<=m;++i)
{
fin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
ok=1;
for(i=1;i<=n;++i)
{
if(G[i].empty() || G[i].size()%2==1)
{
ok=0;
break;
}
}
if(ok==0)
fout<<-1<<"\n";
else
{
now=1;
do
{
Euler(now);
now=st.top();
fout<<now<<" ";
st.pop();
}
while(!st.empty());
}
return 0;
}