Pagini recente » Cod sursa (job #2691439) | Cod sursa (job #2619007) | Cod sursa (job #235186) | Cod sursa (job #2975278) | Cod sursa (job #1308694)
///EULERIAN CYCLE HOME 04.01
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <stack>
using namespace std;
list<int> cycle;
vector<list<int> > adjList;
void itEuler(vector<list<int> >& adjList, list<int>& cycle) {
stack<int> s;
s.push(0);
int current, next;
list<int>::iterator it;
while(!s.empty()) {
current = s.top();
if(adjList[current].empty()) {
s.pop();
cycle.push_back(current);
continue;
}
next = adjList[current].front();
adjList[current].pop_front();
s.push(next);
for(it = adjList[next].begin(); it != adjList[next].end(); it++)
if(*it == current) {
adjList[next].erase(it);
break;
}
}
}
void recEuler(int v) {
while(!adjList[v].empty()) {
int w = adjList[v].front();
adjList[v].pop_front();
for(list<int>::iterator it = adjList[w].begin(); it != adjList[w].end(); it++)
if(*it == v) {
adjList[w].erase(it);
break;
}
recEuler(w);
}
cycle.push_back(v);
}
int main() {
ifstream inStr("ciclueuler.in");
ofstream outStr("ciclueuler.out");
int N, M;
inStr >> N >> M;
adjList.resize(N);
vector<int> numEdges(N, 0);
int from, to;
for(int i=0; i<M; i++) {
inStr >> from >> to;
adjList[--from].push_back(--to);
adjList[to].push_back(from);
++numEdges[from];
++numEdges[to];
}
for(int i=0; i<N; i++)
if(numEdges[i] % 2 != 0) {
cout << -1 << '\n';
return 0;
}
recEuler(0);
///itEuler(adjList, cycle);
for(list<int>::iterator it = cycle.begin();
it != cycle.end(); it++)
outStr << *it + 1 << ' ';
outStr << '\n';
return 0;
}