fork download
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>
  4.  
  5. struct node
  6. {
  7. int nodeNumber;
  8. int childCount;
  9. struct node** childArray;
  10. };
  11.  
  12. typedef struct node Node;
  13.  
  14. struct input
  15. {
  16. int start;
  17. int end;
  18. struct input* next;
  19. };
  20.  
  21. typedef struct input Input;
  22.  
  23. int PrintLoop(int array[],int currentCount,int maxLength,Node* head)
  24. {
  25. int currentNode = head->nodeNumber;
  26. int i = 0,j=0,k=-1;
  27. for(i=0;i<maxLength;i++)
  28. {
  29. printf("%d\t",array[i]);
  30. }
  31. printf("\n\n");
  32. for(i=0;i<currentCount;i++)
  33. {
  34. if(array[i]==currentNode)
  35. break;
  36. }
  37. if(i==currentCount)
  38. {
  39. array[i] = currentNode;
  40. currentCount++;
  41. for(j=0;j<head->childCount;j++)
  42. {
  43. k = PrintLoop(array,currentCount,maxLength,head->childArray[j]);
  44. if(k==1)
  45. break;
  46. }
  47. }
  48. else
  49. {
  50. int minIndex = i,l=i;
  51. int minNumber = array[i];
  52. while(l<currentCount)
  53. {
  54. if(array[l]<minNumber)
  55. {
  56. minIndex = l;
  57. minNumber = array[l];
  58. }
  59. l++;
  60. }
  61.  
  62. printf("%d",minNumber);
  63. if(minIndex!=currentCount-1)
  64. {
  65. l = minIndex+1;
  66. }
  67. else
  68. {
  69. l = i;
  70. }
  71.  
  72.  
  73.  
  74. while(l!=minIndex)
  75. {
  76. printf("->%d",array[l]);
  77. if(l!=currentCount-1)
  78. {
  79. l++;
  80. }
  81. else
  82. {
  83. l = i;
  84. }
  85. }
  86. printf("->%d",array[l] );
  87. k = 1;
  88. }
  89. return k;
  90. }
  91.  
  92.  
  93. int main(void)
  94. {
  95. int i,j;
  96. Input *inputHead=NULL,*tempInput=NULL,*lastInput=NULL;
  97. int input;
  98. scanf("%d",&input);
  99. Node** nodeArray = (Node**)malloc(input*sizeof(Node*));
  100. for(i=0;i<input;i++)
  101. {
  102. Node* temp = (Node*)malloc(sizeof(Node));
  103. temp->nodeNumber = 0;
  104. temp->childCount = 0;
  105. temp->childArray = NULL;
  106. nodeArray[i] = temp;
  107. }
  108.  
  109. i=0;
  110. while(i!=-1)
  111. {
  112. scanf("%d",&i);
  113. if(i!=-1)
  114. {
  115. tempInput= (Input*)malloc(sizeof(Input));
  116. tempInput->start = i;
  117. nodeArray[i]->childCount++;
  118. tempInput->next=NULL;
  119. scanf("%d",&i);
  120. tempInput->end = i;
  121.  
  122. if(inputHead==NULL)
  123. {
  124. inputHead = tempInput;
  125. lastInput = tempInput;
  126. }
  127. else
  128. {
  129. lastInput->next = tempInput;
  130. lastInput = tempInput;
  131. }
  132. }
  133.  
  134. }
  135.  
  136. for(i=0;i<input;i++)
  137. {
  138. nodeArray[i]->childArray = (Node**)malloc(nodeArray[i]->childCount*sizeof(Node*));
  139. nodeArray[i]->nodeNumber = nodeArray[i]->childCount-1;
  140. }
  141.  
  142. while(inputHead!=NULL)
  143. {
  144. tempInput = inputHead;
  145. nodeArray[tempInput->start]->childArray[nodeArray[tempInput->start]->nodeNumber] = nodeArray[tempInput->end];
  146. nodeArray[tempInput->start]->nodeNumber--;
  147. inputHead = inputHead->next;
  148. free(tempInput);
  149. }
  150. int *array = (int*)malloc(input*sizeof(int));
  151. for(i=0;i<input;i++)
  152. {
  153. nodeArray[i]->nodeNumber = i;
  154. array[i] = -1;
  155. }
  156. j=-1;
  157. for(i=0;i<input;i++)
  158. {
  159. j = PrintLoop(array,0,input,nodeArray[i]);
  160. if(j==1)
  161. {
  162. break;
  163. }
  164. }
  165.  
  166. if(j==-1)
  167. {
  168. printf("No Loop");
  169. }
  170. return 0;
  171. }
  172.  
Success #stdin #stdout 0.01s 1856KB
stdin
5
0 1
1 2
2 3
3 4
1 0
-1
stdout
-1	-1	-1	-1	-1	

0	-1	-1	-1	-1	

0	1	-1	-1	-1	

0->1->0