Pagini recente » Cod sursa (job #2465277) | Cod sursa (job #738783) | Cod sursa (job #437412) | Cod sursa (job #438447) | Cod sursa (job #2641324)
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
#include <algorithm>
std::ifstream fin("ciclueuler.in");
std::ofstream fout("ciclueuler.out");
const int maxn = 100005;
int n, m;
std::vector <int> g[maxn];
inline void euler(int stnode)
{
std::stack <int> st;
st.push(stnode);
std::vector <int> cycle;
while (!st.empty())
{
int node = st.top();
if (!g[node].empty())
{
int newnode = g[node].back();
g[node].pop_back();
g[newnode].erase(find(g[newnode].begin(), g[newnode].end(), node));
st.push(newnode);
}
else
{
st.pop();
if (!st.empty())
cycle.push_back(node);
}
}
for (auto it : cycle)
fout << it << ' ';
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
int x, y;
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; ++i)
{
if (g[i].empty() || g[i].size() & 1)
{
fout << "-1\n";
return 0;
}
}
euler(1);
}