Pagini recente » Cod sursa (job #2495317) | Concursuri organizate de infoarena | Cod sursa (job #333065) | Cod sursa (job #1703746) | Cod sursa (job #3253450)
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
ifstream in("ciclueuler.in"); ofstream out("ciclueuler.out");
vector <int> lista[100001];
bool ok[500001];
stack<int> st;
void euler(int nod) {
int fiu;
while (!lista[nod].empty()){
st.push(nod);
fiu = lista[nod].back();
lista[nod].pop_back();
for (auto it = lista[fiu].begin(); it != lista[fiu].end(); ++it) {
if (*it == nod) {
lista[fiu].erase(it);
break;
}
}
nod = fiu;
}
}
int main() {
int n, m, nod1, nod2;
in >> n >> m;
for (int i = 1; i <= m; i++) {
in >> nod1 >> nod2;
lista[nod1].push_back(nod2);
lista[nod2].push_back(nod1);
}
bool possible = true;
for (int i = 1; i <= n; i++) {
if (lista[i].size() % 2 == 1) {
possible = false;
break;
}
}
if (!possible) {
out << "-1";
} else {
int nod = 1;
bool finished = false;
while (!st.empty() || !finished) {
finished = true;
out << nod << " ";
euler(nod);
// if (!st.empty()) { /// imi afiseaza si ultimul element (primul din nou)
nod = st.top();
st.pop();
//finished = false;
//}
}
}
return 0;
}