Pagini recente » Cod sursa (job #1307702) | Cod sursa (job #194713) | Cod sursa (job #2127153) | Cod sursa (job #750138) | Cod sursa (job #2817730)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
const int MAXN=100001 ,MAXM = 500001;
int N,M;
vector<int> G[MAXN],ANS;
void elimin(int x,int edge)
{
G[edge].pop_back();
vector<int>::iterator it;
for (it=G[x].begin(); it!=G[x].end(); it++)
if (*it==edge)
{
G[x].erase(it);
return;
}
}
void ciclueuler()
{
int x, edge = 1;
for (int i=1; i<=N; i++)
if (G[i].size() == 1)
{
out<<"-1\n";
return ;
}
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()
{
int x,y;
in>>N>>M;
while (M--)
{
in>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
ciclueuler();
ANS.pop_back();
for (auto it : ANS)
out<<it<<' ';
return 0;
}