Pagini recente » Cod sursa (job #1922588) | Cod sursa (job #2878959) | Cod sursa (job #2128438) | Cod sursa (job #1168888) | Cod sursa (job #3004004)
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <tuple>
using namespace std;
ifstream cin("ciclueuler.in");
ofstream cout("ciclueuler.out");
using PI = pair<int, int>;
using VI = vector<int>;
using VB = vector<bool>;
using VP = vector<PI>;
using Iter= VP::iterator;
using VVP = vector<VP>;
int n, m;
VVP G;
VB v;
VI c;
struct Egde {
int x, y;
};
void CitesteDate();
bool Eulerian();
void Ciclu(int x);
void AfisCiclu();
int main()
{
CitesteDate();
if (!Eulerian())
cout << "-1";
else
{
Ciclu(1);
AfisCiclu();
}
}
void Ciclu(int x)
{
vector<Iter> p(n + 1);
stack<int> stk;
for (int i = 1; i <= n; ++i)
p[i] = G[i].begin();
stk.push(x);
int y, e;
while (!stk.empty())
{
x = stk.top();
if (p[x] == G[x].end())
{
c.push_back(x);
stk.pop();
}
else
{
tie(e, y) = *p[x]++;
if (v[e]) continue;
v[e] = true;
stk.push(y);
}
}
}
void AfisCiclu()
{
for (int x : c)
cout << x << ' ';
}
bool Eulerian()
{
for (int x = 1; x <= n; ++x)
if (G[x].size() % 2 == 1)
return false;
return true;
}
void CitesteDate()
{
cin >> n >> m;
G = VVP(n + 1);
v = VB(n + 1);
int x, y;
for (int e = 1; e <= m; ++e)
{
cin >> x >> y;
G[x].emplace_back(e, y);
G[y].emplace_back(e, x);
}
}