Pagini recente » Cod sursa (job #2917976) | Cod sursa (job #2422449) | Cod sursa (job #3147637) | Cod sursa (job #1984611) | Cod sursa (job #2259726)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
#define MAX 100010
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
#define cout g
vector<int> G[MAX];
vector<int> sol;
stack<int> s;
void euler(int node_arg)
{
s.push(node_arg);
while (!s.empty())
{
int node = s.top();
if (!G[node].empty())
{
int nnode = G[node][0];
G[node].erase(G[node].begin());
G[nnode].erase(find(G[nnode].begin(), G[nnode].end(), node));
s.push(nnode);
continue;
}
sol.push_back(node);
s.pop();
}
}
int main()
{
int n, m;
f >> n >> m;
for (int i = 0; i < m; ++i)
{
int x, y;
f >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
bool eulerian = true;
for (int i = 1; i <= n; ++i)
{
if (G[i].size() % 2 != 0)
{
eulerian = false;
}
}
if (eulerian)
{
euler(1);
if (sol.size() - 1 == m)
{
for (int i = 0; i < sol.size() - 1; ++i)
{
cout << sol[i] << ' ';
}
}
else
{
cout << -1 << '\n';
}
}
else
{
cout << -1 << '\n';
}
return 0;
}