fork(1) download
  1. #define MAX 10
  2. #define inputfile "test.txt"
  3. // file contents
  4. /*
  5. 5
  6. 0 1 1 0 0
  7. 1 0 1 1 1
  8. 1 1 0 1 1
  9. 0 1 1 0 1
  10. 0 1 1 1 0
  11. */
  12.  
  13. #include<stdio.h>
  14. typedef struct STACK {
  15. int array[100];
  16. int size;
  17. } STACK;
  18. typedef struct GRAPH {
  19. int n;
  20. int a[MAX][MAX];
  21. } DOTHI;
  22.  
  23. int DocMaTranKe(char test[100], DOTHI *g) {
  24. FILE* f = stdin;
  25. /*FILE *f = fopen(test, "rt");
  26. f = fopen(f, "r");
  27. if (f == NULL) {
  28. printf("Khong mo duoc file\n");
  29. return 0;
  30. }*/
  31. fscanf(stdin, "%d", &g->n);
  32. int i, j;
  33. for (i=0; i < g->n; i++) {
  34. for (j=0; j < g->n; j++) {
  35. fscanf(stdin, "%d", &g->a[i][j]);
  36. }
  37. }
  38. fclose(f);
  39. return 1;
  40. }
  41.  
  42. void XuatMaTranKe (DOTHI g) {
  43. printf("So dinh cua do thi la %d\n", g.n);
  44. printf("Ma tran ke cua do thi la\n");
  45. for (int i = 0; i < g.n; i++) {
  46. printf ("\t");
  47. for (int j = 0; j < g.n; j++) {
  48. printf("%d ",g.a[i][j]);
  49. }
  50. printf("\n");
  51. }
  52. }
  53.  
  54. void khoitaoStack(STACK *stack) {
  55. stack->size = 0;
  56. }
  57.  
  58. void DayGiaTriVaoStack (STACK *stack, int value) {
  59. if(stack->size + 1 >= 100) {
  60. return;
  61. }
  62. stack->array[stack->size] = value;
  63. stack->size++;
  64. }
  65.  
  66. void TimDuongDi (DOTHI *g, STACK *stack, int i) {
  67. for (int j = 0; j < g->n ; j++) {
  68. if (g->a[i][j] != 0) {
  69. g->a[i][j] = g->a[j][i] = 0;
  70. // XuatMaTranKe(*g);
  71. TimDuongDi(g,stack,j);
  72. // break;
  73. }
  74. }
  75. DayGiaTriVaoStack(stack,i);
  76. }
  77.  
  78. int KiemTraChuTrinhEuler (DOTHI g) {
  79. int i, j, bac;
  80. for (i = 0; i < g.n; i++) {
  81. bac = 0;
  82. for (j = 0; j < g.n; j++) {
  83. bac += g.a[i][j];
  84. }
  85. if (bac % 2)
  86. return 0;
  87. }
  88. int x = 0;
  89. int flag = 0;
  90. for (i = 0; i < g.n; i++) {
  91. for (j = 0; j < g.n; j++) {
  92. if (g.a[i][j] != 0) {
  93. x = i;
  94. flag = 1;
  95. break;
  96. }
  97. }
  98. if(flag==1)
  99. break;
  100. }
  101. DOTHI temp = g;
  102. STACK stack;
  103. khoitaoStack(&stack);
  104. TimDuongDi(&temp, &stack, x);
  105. for (i = 0; i < temp.n; i++) {
  106. for (j = 0; j < temp.n; j++) {
  107. if (temp.a[i][j] != 0)
  108. return 0;
  109. }
  110. }
  111. if (stack.array[stack.size - 1] != stack.array[0])
  112. return 0;
  113. printf("\nChu trinh Euler: ");
  114. for(i = stack.size - 1; i >= 0 ; i--)
  115. printf("%d ",stack.array[i] + 1);
  116. return 1;
  117. }
  118.  
  119. int KiemTraDuongDiEuler (DOTHI g) {
  120. int i,j;
  121. int x = 0;
  122. int flag = 0;
  123. int bac = 0;
  124. for (i = 0; i < g.n; i++) {
  125. bac = 0;
  126. for (j = 0; j < g.n; j++) {
  127. if (g.a[i][j] != 0) {
  128. bac++;
  129. }
  130. }
  131. if (bac%2 != 0) {
  132. x = i;
  133. flag = 1;
  134. break;
  135. }
  136. }
  137. if(flag == 0)
  138. return 0;
  139. DOTHI temp = g;
  140. STACK stack;
  141. khoitaoStack (&stack);
  142. TimDuongDi(&temp, &stack,x);
  143. for (i = 0; i < temp.n; i++) {
  144. for (j = 0; j < temp.n; j++) {
  145. if(temp.a[i][j]!=0)
  146. return 0;
  147. }
  148. }
  149. if (stack.array[stack.size - 1] == stack.array[0])
  150. return 0;
  151. printf("\nDuong di Euler : ");
  152. for(i = stack.size - 1; i >= 0 ; i--)
  153. printf("%d ",stack.array[i] + 1);
  154. return 1;
  155. }
  156.  
  157. int main() {
  158. // makefile(5);
  159. DOTHI g;
  160. if (DocMaTranKe(inputfile, &g) == 1) {
  161. printf("Da lay thong tin do thi tu file thanh cong.\n\n");
  162. XuatMaTranKe(g);
  163. printf("Bam 1 phim bat ki de bat dau xet tim chu trinh euler ...\n\n");
  164. if (!KiemTraChuTrinhEuler(g)) {
  165. printf("Khong co chu trinh Euler trong do thi cua ban\n");
  166. printf("\nBam 1 phim bat ki de bat dau xet tim duong di euler ...\n");
  167. if (!KiemTraDuongDiEuler(g)) {
  168. printf("\nKhong co duong di Euler trong do thi cua ban \n");
  169. }
  170. }
  171. }
  172. return 0;
  173. }
Success #stdin #stdout 0s 9424KB
stdin
5
0	1	1	0	0
1	0	1	1	1
1	1	0	1	1
0	1	1	0	1
0	1	1	1	0
stdout
Da lay thong tin do thi tu file thanh cong.

So dinh cua do thi la 5
Ma tran ke cua do thi la
	0 1 1 0 0 
	1 0 1 1 1 
	1 1 0 1 1 
	0 1 1 0 1 
	0 1 1 1 0 
Bam 1 phim bat ki de bat dau xet tim chu trinh euler ...

Khong co chu trinh Euler trong do thi cua ban

Bam 1 phim bat ki de bat dau xet tim duong di euler ...

Duong di Euler : 4 2 1 3 2 5 3 4 5