Pagini recente » Cod sursa (job #3206601) | Cod sursa (job #2635982) | Cod sursa (job #706244) | Cod sursa (job #1263911) | Cod sursa (job #3226782)
#include <fstream>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
ifstream cin ("ciclueuler.in");
ofstream cout ("ciclueuler.out");
using pa = pair <int, int>;
const int M = 5e5;
const int N = 1e5;
bool degree[N + 1];
bool viz[M + 1];
vector <pa> g[N + 1];
stack <int> s;
vector <int> sol;
struct ceva
{
int x, y, cost;
};
vector <ceva> edges;
int n, m, x, y;
int main()
{
cin >> n >> m;
for (int i = 1; i <= m; ++i)
{
cin >> x >> y;
degree[x] ^= 1;
degree[y] ^= 1;
edges.push_back({x, y, i});
edges.push_back({y, x, i});
g[x].push_back({y, i});
g[y].push_back({x, i});
}
for (int i = 1; i <= n; ++i)
if (degree[i])
{
cout << "-1\n";
return 0;
}
s.push(1);
while (!s.empty())
{
int x = s.top();
if (!g[x].empty())
{
int node = g[x].front().first;
int muchie = g[x].front().second;
g[x].erase (g[x].begin());
if (!viz[muchie])
viz[muchie] = 1, s.push(node);
}
else
sol.push_back(x), s.pop();
}
sol.erase (sol.begin());
reverse (sol.begin(), sol.end());
for (auto it : sol)
cout << it << ' ';
return 0;
}