Pagini recente » Cod sursa (job #2542277) | Cod sursa (job #2089224) | Cod sursa (job #3219434) | Cod sursa (job #224510) | Cod sursa (job #2641323)
#include <fstream>
#include <algorithm>
#include <stack>
#include <vector>
class InParser
{
#pragma warning(disable:4996)
private:
FILE* fin;
char* buff;
int sp;
char read()
{
++sp;
if (sp == 4096)
{
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume)
{
sp = 4095;
buff = new char[4096];
fin = fopen(nume, "r");
}
InParser& operator >> (int& n)
{
int sgn = 1;
char c;
while (!isdigit(c = read()) && c != '-');
if (c == '-')
{
n = 0;
sgn = -1;
}
else
n = c - '0';
while (isdigit(c = read()))
n = n * 10 + c - '0';
n *= sgn;
return *this;
}
};
InParser fin("ciclueuler.in");
std::ofstream fout("ciclueuler.out");
const int NMAX = 1e5 + 2;
std::vector <int> g[NMAX];
void euler()
{
std::stack <int> st;
st.push(1);
std::vector <int> cycle;
while (!st.empty())
{
int node = st.top();
if (!g[node].empty())
{
int new_node = g[node].back();
g[node].pop_back();
g[new_node].erase(std::find(g[new_node].begin(), g[new_node].end(), node));
st.push(new_node);
}
else
{
st.pop();
if (!st.empty())
cycle.push_back(st.top());
}
}
for (auto i : cycle)
fout << i << " ";
fout << "\n";
}
int n, x, y, m;
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; ++i)
if (!g[i].size() || g[i].size() & 1)
{
fout << "-1\n";
return 0;
}
euler();
fout.close();
return 0;
}