Pagini recente » Cod sursa (job #961891) | Cod sursa (job #993875) | Cod sursa (job #1270098) | Cod sursa (job #1280864) | Cod sursa (job #2931776)
#include <fstream>
#include <vector>
#include <bitset>
#include <stack>
using namespace std;
ifstream cin ("ciclueuler.in");
ofstream cout ("ciclueuler.out");
const int M = 5e5;
const int N = 1e5;
struct edge{
int id, u, v;
};
bitset <M + 1> viz;
vector <edge> 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 (edge {++k, x, y}), g[y].push_back({k, y, 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().id;
int urm = g[node].back().v;
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;
}