Pagini recente » Cod sursa (job #3171730) | Cod sursa (job #1882161) | Cod sursa (job #234082) | Cod sursa (job #2718154) | Cod sursa (job #1143134)
#include <fstream>
#include <vector>
#include <sys/resource.h>
using namespace std;
ifstream in ("ciclueuler.in");
ofstream out ("ciclueuler.out");
const int N = 500045;
struct Muchie {
int first;
int second;
};
Muchie getMuchie (int x, int y)
{
Muchie m;
m.first = x;
m.second = y;
return m;
}
int noduri, muchii;
vector <int> indici[N];
bool folosit[N];
vector <int> ciclu;
bool vizitat[N];
Muchie v[N];
void changeStackSize()
{
const rlim_t kStackSize = 32 * 1024 * 1024;
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if (!result) {
if (rl.rlim_cur < kStackSize) {
rl.rlim_cur = kStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result) {
out << "setrlimit returned result = " << stderr << "\n";
}
}
}
}
void read()
{
in >> noduri >> muchii;
for (int i = 1; i <= muchii; ++i) {
int x, y;
in >> y >> x;
indici[x].push_back(i);
indici[y].push_back(i);
v[i] = getMuchie(x, y);
}
}
inline int varf (int nod, int valoare)
{
if (v[nod].first == valoare) {
return v[nod].second;
}
return v[nod].first;
}
inline void euler (int x)
{
for (auto it = indici[x].begin(); it < indici[x].end(); ++it) {
int indice = *it;
if (!folosit[indice]) {
folosit[indice] = true;
int y = varf(indice, x);
euler(y);
}
}
ciclu.push_back(x);
}
void dfs (int x)
{
vizitat[x] = true;
for (auto it = indici[x].begin(); it < indici[x].end(); ++it) {
int y = varf(*it, x);
if (!vizitat[y]) {
dfs(y);
}
}
}
bool check()
{
dfs(1);
for (int i = 1; i <= noduri; ++i) {
if (!vizitat[i] or indici[i].size() % 2) {
return false;
}
}
return true;
}
void solve ()
{
bool ok = check();
if (!ok) {
out << "-1\n";
return;
}
euler(1);
if (ciclu.size() == muchii + 1) {
for (int i = 0; i < muchii; ++i) {
out << ciclu[i] << " ";
}
out << "\n";
} else {
out << "-1\n";
}
}
int main()
{
read();
solve();
return 0;
}