Pagini recente » Cod sursa (job #1051487) | Cod sursa (job #2495472) | Cod sursa (job #1738179) | Cod sursa (job #1594895) | Cod sursa (job #2076269)
#include<fstream>
#include<list>
#include<stack>
#include<deque>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
const int nmax = 100005;
list<int>l[nmax];
stack<int>s;
deque<int>sol;
int n, m;
bool viz[nmax];
void citire(){
f >> n >> m;
int x, y;
for(int i = 1; i <= m; i++){
f >> x >> y;
l[x].push_back(y);
l[y].push_back(x);
}
}
void euler()
{
int nod_curent, nod_urm;
s.push(1);
while(!s.empty()){
nod_curent = s.top();
if(l[nod_curent].size()){
nod_urm = l[nod_curent].front();
l[nod_curent].pop_front();
list<int>::iterator it;
for(it = l[nod_urm].begin(); *it != nod_curent; it++);
l[nod_urm].erase(it);
s.push(nod_urm);
}
else{
sol.push_back(nod_curent);
s.pop();
}
}
}
void afisare(){
while(!sol.empty()){
g << sol.front() << ' ';
sol.pop_front();
}
}
bool eulerian(){
list<int>::iterator it;
for(int i = 1; i <= n; i++){
if(!(l[i].size() and l[i].size()%2))
return 0;
}
return 1;
}
int main(){
citire();
if(eulerian()){
euler();
afisare();
}
else
g << -1;
}