Pagini recente » Cod sursa (job #44322) | Cod sursa (job #887173) | Cod sursa (job #2652559) | Cod sursa (job #1969394) | Cod sursa (job #1142660)
#include <fstream>
#include <vector>
#include <sys/resource.h>
using namespace std;
ifstream in ("ciclueuler.in");
ofstream out ("ciclueuler.out");
const int N = 100009;
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*5];
int ciclu[N*5];
int elementeCiclu;
bool vizitat[N*5];
Muchie v[N*5];
void read()
{
in >> noduri >> muchii;
for (int i = 1; i <= muchii; ++i) {
int x, y;
in >> x >> y;
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 (vector<int>::iterator 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[++elementeCiclu] = x;
}
void dfs (int x)
{
vizitat[x] = true;
for (auto it = indici[x].begin(); it < indici[x].end(); ++it) {
int y = *it;
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 modificaStiva()
{
const rlim_t kStackSize = 32 * 1024 * 1024; // min stack size = 32 MB
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0)
{
if (rl.rlim_cur < kStackSize)
{
rl.rlim_cur = kStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result != 0)
{
fprintf(stderr, "setrlimit returned result = %d\n", result);
}
}
}
}
void solve ()
{
modificaStiva();
bool ok = check();
//out << ok << "\n";
if (!ok) {
out << "-1\n";
return;
}
euler(1);
//out << elementeCiclu << "\n";
if (elementeCiclu == muchii + 1) {
for (int i = 1; i <= muchii; ++i) {
out << ciclu[i] << " ";
}
out << "\n";
} else {
out << "-1\n";
}
}
int main()
{
read();
solve();
return 0;
}