Pagini recente » Cod sursa (job #2166748) | Cod sursa (job #1538490) | Cod sursa (job #2463182) | Autentificare | Cod sursa (job #1343710)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define nmax 100005
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m, grad[nmax];
vector <int> G[nmax], sol;
stack <int> st;
void read() {
int x, y;
fin >> n >> m;
for (int i = 1; i <= m; i++) {
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
grad[x]++;
grad[y]++;
}
}
bool check() {
for (int i = 1; i <= n; i++)
if (grad[i] % 2 == 1)
return 0;
return 1;
}
void sterge(int nod, int vc) {
vector <int>::iterator it;
for (it = G[nod].begin(); it != G[nod].end(); it++)
if (*it == vc) {
G[nod].erase(it);
return;
}
}
void euler(int nod) {
st.push(nod);
while (!st.empty()) {
nod = st.top();
if (G[nod].size() != 0) {
int vc = G[nod][0]; G[nod].erase(G[nod].begin());
sterge(vc, nod); // il sterg din G[vc], pe nod
st.push(vc);
} else {
sol.push_back(st.top());
st.pop();
}
}
}
void solve() {
if (check() == 0) {
fout << -1 << "\n";
return;
}
euler(1);
for (int i = 0; i < m; ++i)
fout << sol[i] << " ";
fout << "\n";
}
int main() {
read();
solve();
fin.close();
fout.close();
return 0;
}