Pagini recente » Cod sursa (job #2252446) | Cod sursa (job #83306) | Cod sursa (job #1086485) | Cod sursa (job #405513) | Cod sursa (job #1435154)
#include<fstream>
#include<algorithm>
#include<list>
#include<deque>
#define N 100010
using namespace std;
ofstream g("ciclueuler.out");
list<int> v[N];
list<int>::iterator it;
deque<int> Q;
int n, m, i, x, y, ok;
void euler(int nd)
{
int nod, nod2;
Q.push_front(nd);
while(!Q.empty())
{
nod = Q.front();
if(v[nod].size()==0)
{
g << nod << " ";
Q.pop_front();
continue;
}
nod2 = v[nod].front();
v[nod].pop_front();
Q.push_front(nod2);
for(it = v[nod2].begin(); it != v[nod2].end(); it++)
if(*it == nod)
{
v[nod2].erase(it);
break;
}
}
}
int main()
{
ifstream f("ciclueuler.in");
f >> n >> m;
for(i = 1; i <= m; i++)
{
f >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
ok=1;
for(i = 1; i <= n; i++)
if(v[i].size() & 1)
{
ok=0;
break;
}
if(!ok) g << "-1\n";
else
euler(1);
f.close();
g.close();
return 0;
}