Advertisement
vinocastro

ME02

Oct 18th, 2020 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node Node;
  5. struct node{
  6.     int SUCC;
  7.     Node* NEXT;
  8. };
  9.  
  10. void print(int n, int *COUNT, Node **LIST){
  11.     Node *alpha;
  12.     printf("num_vertex: %d\n", n);
  13.  
  14.     printf("------------------\n");
  15.     for(int i=0; i<n; i++){
  16.         printf("COUNT[%d]: %d\n", i+1, COUNT[i]);
  17.         printf("\tLINK[%d]: ", i+1);
  18.         alpha=LIST[i];
  19.         while(alpha != NULL){
  20.             printf(" %d", alpha->SUCC);
  21.             alpha = alpha->NEXT;
  22.         }
  23.         printf("\n");
  24.     }
  25. }
  26.  
  27. int main()
  28. {
  29.     int n;
  30.     scanf("%d",&n);
  31.     int COUNT[n];
  32.     Node* LIST[n];
  33.     Node* alpha = NULL;
  34.    
  35.     for(int k = 0;k<=n;k++)
  36.     {
  37.         COUNT[k] = 0;
  38.         LIST[k] =  NULL;
  39.     }
  40.     int m = n;
  41.     int j,k;
  42.     scanf("%d %d",&j,&k);
  43.     while(j != 0 && k != 0)
  44.     {  
  45.         alpha = (Node*) malloc(sizeof(Node));
  46.         COUNT[k]++;
  47.         alpha->SUCC = k;
  48.         alpha->NEXT = LIST[j];
  49.         LIST[j] = alpha;
  50.         scanf("%d %d",&j,&k);
  51.     }
  52.     int front = 0,rear = 0;
  53.     for(int k = 0;k<=n;k++)
  54.     {
  55.         if(COUNT[k]==0)
  56.         {
  57.             if(front==0)
  58.             {
  59.                 front = k;
  60.                 rear = k;
  61.             }
  62.             else
  63.             {
  64.                 COUNT[rear] = k;
  65.                 rear = k;
  66.             }
  67.            
  68.         }
  69.     }
  70.  
  71.     while(front != 0)
  72.     {
  73.         printf("%d ",front);
  74.         m--;
  75.         alpha = LIST[front];
  76.         while(alpha != NULL)
  77.         {
  78.             k = alpha->SUCC;
  79.             COUNT[k]--;
  80.             if(COUNT[k] == 0)
  81.             {
  82.                 COUNT[rear] = k;
  83.                 rear = k;
  84.             }
  85.             alpha = alpha->NEXT;
  86.         }
  87.         front = COUNT[front];
  88.     }
  89.     if(m > 0)
  90.     {
  91.         printf("Some objects comprise a loop");
  92.     }
  93.  
  94.  
  95.     return 0;
  96. }
  97.  
  98.  
  99.  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement