Pagini recente » Rezultatele filtrării | Cod sursa (job #1901369)
#include <stdio.h>
struct pair {
int x, y;
};
struct node {
int v;
node* n;
node() {v = 0; n = NULL;}
};
node* all() { // Alocator de memorie
static int sp = 5;
static node* st = NULL;
if (sp == 5) {
st = new node[5];
sp = 0;
}
return st + (sp++);
}
FILE *o;
node** G;
pair* E;
bool* viz;
int cnt;
int wee = 0;
void dfs(int u)
{
while (G[u] != NULL) {
//printf("%d %d\n", wee++, G[u]->v);
if (!viz[G[u]->v]) {
viz[G[u]->v] = 1;
int loc = G[u]->v;
printf("A %p\n", G[u]);
G[u] = G[u]->n;
printf("B %d\n", loc);
dfs(E[loc].x ^ E[loc].y ^ u);
} else {
printf("C\n");
G[u] = G[u]->n;
printf("D\n");
}
}
if (cnt) {
fprintf(o, "%d ", u);
--cnt;
}
}
int main()
{
FILE *fin, *fout;
fin = fopen("ciclueuler.in", "r");
fout = fopen("ciclueuler.out", "w");
o = fout;
// Citesc
int N, M;
fscanf(fin, "%d%d", &N, &M);
cnt = M;
G = new node*[N + 1]();
E = new pair[M];
viz = new bool[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;
node *fwd = all(), *bwd = all();
fwd->v = i; bwd->v = i;
fwd->n = G[x]; bwd->n = G[y];
// Bag in liste
G[x] = fwd; G[y] = bwd;
}
dfs(1);
fclose(fin);
fclose(fout);
return 0;
}