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