Pagini recente » Cod sursa (job #732156) | Cod sursa (job #2300294) | Cod sursa (job #1577548) | Cod sursa (job #3277307) | Cod sursa (job #1919481)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");
const int maxn = 1e5 + 5;
vector <int> G[maxn];
bool Is_Euler (int n) {
for (int i = 1; i <= n; i++) {
if (G[i].size() & 1) return false;
}
return true;
}
void Euler () {
stack <int> Stk;
int node, son;
Stk.push(1);
while (!Stk.empty()) {
node = Stk.top();
while (!G[node].empty()) {
son = G[node][0];
G[node].erase(G[node].begin());
G[son].erase(find(G[son].begin(), G[son].end(), node));
Stk.push(son);
node = son;
}
fout << Stk.top() << " ";
Stk.pop();
}
}
int main() {
ios_base :: sync_with_stdio (false);
int n, m, x, y, i;
fin >> n >> m;
for (i = 1; i <= m; i++) {
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
if (!Is_Euler(n)) {
fout << "-1\n";
} else {
Euler();
}
fin.close();
fout.close();
return 0;
}