Cod sursa(job #1900962)

Utilizator BonCipBonciocat Ciprian Mircea BonCip Data 3 martie 2017 17:55:25
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#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 = 500000;
	static node* st = NULL;
	if (sp == 500000) {
		st = new node[500000];
		sp = 0;
	}
	return st + (sp++);
}

FILE *o;
node** G;
pair* E;
bool* viz;
int cnt;

void dfs(int u)
{
	while (G[u] != NULL) {
		printf("A %d %d %p\n", G[u]->v, !viz[G[u]->v], G[u]);
		if (!viz[G[u]->v]) {
			printf("B\n");
			viz[G[u]->v] = 1;
			printf("C\n");
			dfs(E[G[u]->v].x ^ E[G[u]->v].y ^ u);
		}
		if (G[u] != NULL) {
			G[u] = G[u]->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;
}