#include <stdio.h>
#include <malloc.h>
#define MAX 50
struct Node {
int info;
struct Node *next;
};
struct Node *head, *ind, *c;
void readGraph(const char*);
void displayMatrix();
int connex();
int have_degree();
void dfs(int);
int add();
void cycle(struct Node *);
int mat[MAX][MAX], visited[ MAX ];
int num_vertices, num_edges;
FILE *fin, *fout;
//main program
int main() {
readGraph("eulerian.in");
if( connex() ) {
if( have_degree() ) {
head
= (struct Node
*)malloc(sizeof(struct Node
)); head->info = 1;
head->next = 0;
while( add() );
c = head;
while( c ) {
c = c->next;
}
} else {
printf("The Graph does n respect the conditions for parity");
}
} else {
printf("The Graph is not connex"); }
return(0);
};
//@param filename from where we can read the graph onto matrix of adjacence
//@return Void.
void readGraph(const char *filename) {
int i,j;
scanf("%d %d", &num_vertices
, &num_edges
);
for(;num_edges; num_edges--) {
mat[i][j] = mat[j][i] = 1;
}
}
//display the matrix adjacence for debug
//@param void
//@return void
void displayMatrix() {
int i,j;
for(i=1;i<=num_vertices;i++) {
for(j=1;j<=num_vertices;j++) {
}
}
}
//chech if the graph is odd or not
int have_degree() {
int sum,i,j,not = 1;
for(i = 1; i <= num_vertices; i++) {
sum = 0;
for(j = 1; j <= num_vertices; j++) {
sum += mat[i][j];
}
if(sum&1) not = 0;
if(!not) return not;
}
return not;
}
//check whether the graph is connex or not
int connex() {
int i;
dfs(1);
for(i=1;i<=num_vertices;i++)
if(!visited[i])
return 0;
return 1;
}
//Depth First Search Traversal
void dfs(int node) {
int j;
visited[ node ] = 1;
for(j = 1; j <= num_vertices; j++) {
if(mat[ node ][ j ]) {
if(!visited[ j ]) {
dfs( j );
}
}
}
}
int add() {
int node, i, found = 0;
struct Node *ind;
ind = head;
while(ind && !found) {
for(i = 1; i <= num_vertices; i++) {
if(mat[ind->info][ i ] == 1) found = 1;
}
if( !found ) ind = ind->next;
}
if( ind ) {
cycle( ind );
return 1;
}
return 0;
};
void cycle(struct Node *p) {
struct Node *newnode, *nextnode, *basenode;
int node;
basenode = p;
nextnode = p->next;
do{
node = 1;
while(mat[ basenode->info ][ node ] == 0) node++;
mat[basenode->info][ node ] = 0;
mat[ node ][ basenode->info ] = 0;
newnode
= (struct Node
*)malloc(sizeof(struct Node
)); newnode->info = node;
newnode->next = NULL;
basenode->next = newnode;
basenode = newnode;
}while(newnode->info != p->info);
basenode->next = nextnode;
};
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYWxsb2MuaD4KI2RlZmluZSBNQVggNTAKCnN0cnVjdCBOb2RlIHsKCiAgICAgICBpbnQgaW5mbzsKICAgICAgIHN0cnVjdCBOb2RlICpuZXh0Owp9OwoKc3RydWN0IE5vZGUgKmhlYWQsICppbmQsICpjOwoKdm9pZCByZWFkR3JhcGgoY29uc3QgY2hhciopOwp2b2lkIGRpc3BsYXlNYXRyaXgoKTsKaW50IGNvbm5leCgpOwppbnQgaGF2ZV9kZWdyZWUoKTsKdm9pZCBkZnMoaW50KTsKaW50IGFkZCgpOwp2b2lkIGN5Y2xlKHN0cnVjdCBOb2RlICopOwoKaW50IG1hdFtNQVhdW01BWF0sIHZpc2l0ZWRbIE1BWCBdOwoKaW50IG51bV92ZXJ0aWNlcywgbnVtX2VkZ2VzOwoKRklMRSAqZmluLCAqZm91dDsKCi8vbWFpbiBwcm9ncmFtCmludCBtYWluKCkgewoKICAgIHJlYWRHcmFwaCgiZXVsZXJpYW4uaW4iKTsKCiAgICBpZiggY29ubmV4KCkgKSB7CgogICAgICAgICAgaWYoIGhhdmVfZGVncmVlKCkgKSB7CgogICAgICAgICAgICAgaGVhZCA9IChzdHJ1Y3QgTm9kZSopbWFsbG9jKHNpemVvZihzdHJ1Y3QgTm9kZSkpOwogICAgICAgICAgICAgaGVhZC0+aW5mbyA9IDE7CiAgICAgICAgICAgICBoZWFkLT5uZXh0ID0gMDsKCiAgICAgICAgICAgICB3aGlsZSggYWRkKCkgKTsKCiAgICAgICAgICAgICBjID0gaGVhZDsKICAgICAgICAgICAgIHdoaWxlKCBjICkgewogICAgICAgICAgICAgICAgICAgcHJpbnRmKCIlZCAiLCBjLT5pbmZvKTsKICAgICAgICAgICAgICAgICAgIGMgPSBjLT5uZXh0OwogICAgICAgICAgICAgfQoKICAgICAgICAgIH0gZWxzZSB7CgogICAgICAgICAgIHByaW50ZigiVGhlIEdyYXBoIGRvZXMgbiByZXNwZWN0IHRoZSBjb25kaXRpb25zIGZvciBwYXJpdHkiKTsKCiAgICAgICAgICB9CgogICAgfSBlbHNlIHsKCiAgICAgICAgICAgcHJpbnRmKCJUaGUgR3JhcGggaXMgbm90IGNvbm5leCIpOwogICAgfQoKICAgIHJldHVybigwKTsKfTsKCi8vQHBhcmFtIGZpbGVuYW1lIGZyb20gd2hlcmUgd2UgY2FuIHJlYWQgdGhlIGdyYXBoIG9udG8gbWF0cml4IG9mIGFkamFjZW5jZQovL0ByZXR1cm4gVm9pZC4Kdm9pZCByZWFkR3JhcGgoY29uc3QgY2hhciAqZmlsZW5hbWUpIHsKCiAgICAgaW50IGksajsKCiAgICAgc2NhbmYoIiVkICVkIiwgJm51bV92ZXJ0aWNlcywgJm51bV9lZGdlcyk7CgogICAgIGZvcig7bnVtX2VkZ2VzOyBudW1fZWRnZXMtLSkgewoKICAgICAgICAgIHNjYW5mKCIlZCAlZCIsJmksJmopOwogICAgICAgICAgbWF0W2ldW2pdID0gbWF0W2pdW2ldID0gMTsKICAgICB9Cgp9CgovL2Rpc3BsYXkgdGhlIG1hdHJpeCBhZGphY2VuY2UgZm9yIGRlYnVnCi8vQHBhcmFtIHZvaWQKLy9AcmV0dXJuIHZvaWQKdm9pZCBkaXNwbGF5TWF0cml4KCkgewoKICAgICBpbnQgaSxqOwoKICAgICBmb3IoaT0xO2k8PW51bV92ZXJ0aWNlcztpKyspIHsKCiAgICAgICAgIGZvcihqPTE7ajw9bnVtX3ZlcnRpY2VzO2orKykgewoKICAgICAgICAgICAgIHByaW50ZigiJWQgIiwgbWF0W2ldW2pdKTsKICAgICAgICAgfQogICAgICAgICAgICAgcHJpbnRmKCJcbiIpOwogICAgIH0KfQoKLy9jaGVjaCBpZiB0aGUgZ3JhcGggaXMgb2RkIG9yIG5vdAppbnQgaGF2ZV9kZWdyZWUoKSB7CgogICAgaW50IHN1bSxpLGosbm90ID0gMTsKCiAgICBmb3IoaSA9IDE7IGkgPD0gbnVtX3ZlcnRpY2VzOyBpKyspIHsKCiAgICAgICAgc3VtID0gMDsKCiAgICAgICAgZm9yKGogPSAxOyBqIDw9IG51bV92ZXJ0aWNlczsgaisrKSB7CgogICAgICAgICAgICBzdW0gKz0gbWF0W2ldW2pdOwogICAgICAgIH0KCiAgICAgICAgaWYoc3VtJjEpIG5vdCA9IDA7CgogICAgICAgIGlmKCFub3QpIHJldHVybiBub3Q7CiAgICB9CiAgICByZXR1cm4gbm90Owp9CgovL2NoZWNrIHdoZXRoZXIgdGhlIGdyYXBoIGlzIGNvbm5leCBvciBub3QKaW50IGNvbm5leCgpIHsKICAgIGludCBpOwoKICAgIGRmcygxKTsKCiAgICBmb3IoaT0xO2k8PW51bV92ZXJ0aWNlcztpKyspCgogICAgICAgIGlmKCF2aXNpdGVkW2ldKQoKICAgICAgICAgICAgcmV0dXJuIDA7CgogICAgcmV0dXJuIDE7Cn0KCi8vRGVwdGggRmlyc3QgU2VhcmNoIFRyYXZlcnNhbAp2b2lkIGRmcyhpbnQgbm9kZSkgewoKICAgICBpbnQgajsKCiAgICAgdmlzaXRlZFsgbm9kZSBdID0gMTsKCiAgICAgZm9yKGogPSAxOyBqIDw9IG51bV92ZXJ0aWNlczsgaisrKSB7CgogICAgICAgICBpZihtYXRbIG5vZGUgXVsgaiBdKSB7CgogICAgICAgICAgICAgICAgaWYoIXZpc2l0ZWRbIGogXSkgewoKICAgICAgICAgICAgICAgICAgICBkZnMoIGogKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgfQogICAgIH0KfQoKaW50IGFkZCgpIHsKCiAgICBpbnQgbm9kZSwgaSwgZm91bmQgPSAwOwoKICAgIHN0cnVjdCBOb2RlICppbmQ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