Pagini recente » Cod sursa (job #2237207) | Cod sursa (job #10521) | Cod sursa (job #2926821) | Cod sursa (job #293454) | Cod sursa (job #2817737)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
int N,M,i,x,y;
vector<int> G[100001],ANS;
void elimin(int x,int edge)
{
G[edge].pop_back();
for (vector<int>::iterator it=G[x].begin(); it!=G[x].end(); it++)
if (*it==edge)
{
G[x].erase(it);
return;
}
}
void ciclueuler()
{
int x, edge = 1;
stack<int> Stack;
Stack.push(1);
while (!Stack.empty())
{
edge=Stack.top();
if (G[edge].empty())
{
ANS.push_back(edge);
Stack.pop();
}
else
{
x=G[edge].back();
elimin(x,edge);
Stack.push(x);
}
}
}
int main()
{
in>>N>>M;
while (M--)
{
in>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
for (int i=1; i<=N; i++)
if (G[i].size()%2 == 1)
{
out<<"-1\n";
return 0;
}
ciclueuler();
ANS.pop_back();
for (i = 0; i < ANS.size(); i++)
out<<ANS[i]<<' ';
return 0;
}