Pagini recente » Cod sursa (job #2322439) | Cod sursa (job #1188103) | Monitorul de evaluare | Cod sursa (job #96854) | Cod sursa (job #1901854)
#include <stdio.h>
#define SPMAX 50
struct pair {
int x, y;
bool viz;
};
struct node {
pair* p;
node* n;
};
node* all() {
static int sp = SPMAX;
static node* st = NULL;
if (sp == SPMAX) {
sp = 0;
st = new node[SPMAX];
}
return st + (sp++);
}
namespace Var {
FILE *fin, *fout;
int N, M;
node** G; pair* E;
int *a, cnt;
int *stiva, sp;
}
void dfsrec(int u) {
using namespace Var;
while (G[u] != NULL) {
if (!G[u]->p->viz) {
node* curr = G[u];
G[u] = G[u]->n;
curr->p->viz = 1;
dfsrec(curr->p->x ^ curr->p->y ^ u);
} else {
G[u] = G[u]->n;
}
}
a[cnt++] = u;
printf("%d\n", cnt);
}
void dfs(int u) {
using namespace Var;
stiva[sp++] = u;
while (sp) {
int v = stiva[sp-1];
if (G[v]) {
if (!G[v]->p->viz) {
node *curr = G[v];
G[v] = G[v]->n;
curr->p->viz = 1;
stiva[sp++] = curr->p->x ^ curr->p->y ^ v;
} else {
G[v] = G[v]->n;
}
} else {
a[cnt++] = v;
--sp;
}
}
}
void afis() {
using namespace Var;
if (cnt == M + 1 && a[0] == a[M]) {
for (int i = 0; i < M; ++i) {
fprintf(fout, "%d ", a[i]);
}
} else {
fprintf(fout, "-1");
}
fprintf(fout, "\n");
}
int main()
{
using namespace Var;
fin = fopen("ciclueuler.in", "r");
fout = fopen("ciclueuler.out", "w");
// Citire
fscanf(fin, "%d%d", &N, &M);
G = new node*[N + 1](); E = new pair[M];
for (int i = 0; i < M; ++i) {
int x, y;
fscanf(fin, "%d%d", &x, &y);
E[i].x = x; E[i].y = y; E[i].viz = 0;
node *fwd = all(), *bwd = all();
fwd->p = E + i; bwd->p = E + i;
fwd->n = G[x]; G[x] = fwd;
bwd->n = G[y]; G[y] = bwd;
}
// Procesare
a = new int[M + 1];
stiva = new int[M + 1];
dfs(E[0].x);
// Afisare
afis();
fclose(fin);
fclose(fout);
return 0;
}