Advertisement
atishay11

DSA_Lab2_B

Feb 11th, 2021
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int counter=1,source,N,M,time,visited[1000],G[1000][1000];
  4. void DFS(int i)
  5. {
  6.     int j;
  7.     visited[i]=1;
  8.     for(j=0;j<N;j++)
  9.     {
  10.         if(G[i][j]==1&&visited[j]==0)
  11.             DFS(j);
  12.         else if(G[i][j]==1&&visited[j]==1) 
  13.             counter++;
  14.     }
  15. }
  16. int main()
  17. {
  18.     int i,j,v1,v2;  
  19.     scanf("%d %d",&N,&M);  
  20.     for(i=0;i<N;i++)
  21.     {
  22.         for(j=0;j<N;j++)
  23.             G[i][j]=0;
  24.     }
  25.     for(i=0;i<M;i++)
  26.     {  
  27.         scanf("%d %d",&v1,&v2);
  28.         G[v1-1][v2-1]=1;
  29.     }
  30.     for(source=1;source<=N;source++)
  31.         DFS(source-1);
  32.     printf("%d",counter);
  33.     return(0);
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement