Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node Node;
- struct node{
- int SUCC;
- Node* NEXT;
- };
- void print(int n, int *COUNT, Node **LIST){
- Node *alpha;
- printf("num_vertex: %d\n", n);
- printf("------------------\n");
- for(int i=0; i<n; i++){
- printf("COUNT[%d]: %d\n", i+1, COUNT[i]);
- printf("\tLINK[%d]: ", i+1);
- alpha=LIST[i];
- while(alpha != NULL){
- printf(" %d", alpha->SUCC);
- alpha = alpha->NEXT;
- }
- printf("\n");
- }
- }
- int main()
- {
- int n;
- scanf("%d",&n);
- int COUNT[n];
- Node* LIST[n];
- Node* alpha = NULL;
- for(int k = 0;k<=n;k++)
- {
- COUNT[k] = 0;
- LIST[k] = NULL;
- }
- int m = n;
- int j,k;
- scanf("%d %d",&j,&k);
- while(j != 0 && k != 0)
- {
- alpha = (Node*) malloc(sizeof(Node));
- COUNT[k]++;
- alpha->SUCC = k;
- alpha->NEXT = LIST[j];
- LIST[j] = alpha;
- scanf("%d %d",&j,&k);
- }
- int front = 0,rear = 0;
- for(int k = 0;k<=n;k++)
- {
- if(COUNT[k]==0)
- {
- if(front==0)
- {
- front = k;
- rear = k;
- }
- else
- {
- COUNT[rear] = k;
- rear = k;
- }
- }
- }
- while(front != 0)
- {
- printf("%d ",front);
- m--;
- alpha = LIST[front];
- while(alpha != NULL)
- {
- k = alpha->SUCC;
- COUNT[k]--;
- if(COUNT[k] == 0)
- {
- COUNT[rear] = k;
- rear = k;
- }
- alpha = alpha->NEXT;
- }
- front = COUNT[front];
- }
- if(m > 0)
- {
- printf("Some objects comprise a loop");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement