Pagini recente » Cod sursa (job #3225593) | Cod sursa (job #750073) | Cod sursa (job #547010) | Cod sursa (job #123979) | Cod sursa (job #3335876)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define pii pair<int, int>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int NMAX = 1e5 + 5, MMAX = 5e5 + 5;
vector<pii> g[NMAX];
int visitat[MMAX], poz[NMAX];
int main () {
int n, m;
fin >> n >> m;
int x,y, id = 0;
for (int i = 0; i < m; i++) {
fin >> x >> y;
g[x].push_back({y, id});
g[y].push_back({x, id});
id++;
}
for (int i = 1; i <= n; i++) {
if(g[i].size() % 2 == 1) {
fout << -1;
return 0;
}
}
stack<int> st;
vector<int> ciclu;
st.push(1);
while (!st.empty()) {
int nod = st.top();
int ok = 0;
while (poz[nod] < g[nod].size()) {
pii neigh = g[nod][poz[nod]++];
if (visitat[neigh.second] == 0) {
visitat[neigh.second] = 1;
st.push(neigh.first);
ok = 1;
break;
}
}
if (ok == 0) {
ciclu.push_back(nod);
st.pop();
}
}
for (auto it: ciclu) {
fout << it << " ";
}
}