Pagini recente » Cod sursa (job #1977196) | Cod sursa (job #2456116) | Cod sursa (job #141151) | Cod sursa (job #2558159) | Cod sursa (job #2931777)
#include <fstream>
#include <vector>
#include <bitset>
#include <stack>
using namespace std;
ifstream cin ("ciclueuler.in");
ofstream cout ("ciclueuler.out");
using zu = pair <int, int>;
const int M = 5e5;
const int N = 1e5;
bitset <M + 1> viz;
vector <zu> g[N + 1];
vector <int> sol;
int n, m, x, y, k;
stack <int> s;
int main()
{
for (cin >> n >> m; m && cin >> x >> y; --m)g[x].push_back ({++k, y}), g[y].push_back({k, x});
for (int i = 1; i <= n; ++i)
if (g[i].size() & 1)
{
cout << "-1\n";
return 0;
}
int ind = 1;
while (g[ind].empty())++ind;
s.push(ind);
while (!s.empty())
{
int node = s.top();
if (!g[node].empty())
{
int muchie = g[node].back().first;
int urm = g[node].back().second;
g[node].pop_back();
if (!viz[muchie])
viz[muchie] = 1, s.push(urm);
}
else
s.pop(), sol.push_back(node);
}
sol.erase (sol.end() - 1);
for (auto it : sol)cout << it << ' ';
return 0;
}