fork(2) download
  1. #include<bits/stdc++.h>
  2. using namespace std ;
  3. #define MAX 100000
  4. vector<int> edges[MAX] ;
  5. int main()
  6. {
  7. int N,E ;
  8. printf("Number of Nodes: ") ;
  9. scanf("%d",&N) ;
  10. printf("Number of Edges: ") ;
  11. scanf("%d",&E) ;
  12. puts("Node -> Node") ;
  13. int outdeg[N+1] ;
  14. memset(outdeg,0,sizeof(outdeg)) ;
  15. for(int i=1 ; i<=E ; i++)
  16. {
  17. int x, y ;
  18. scanf("%d %d",&x,&y) ;
  19. edges[x].push_back(y) ;
  20. outdeg[y]++ ;
  21. }
  22. for(int i=1 ; i<=N ; i++)
  23. {
  24. int sz = edges[i].size() ;
  25. printf("Node-%d is connected with: ",i) ;
  26. for(int j=0 ; j<sz ; j++)
  27. if(edges[i][j])
  28. printf("%d ",edges[i][j]) ;
  29. printf("\nIndegree: %d & Outdegree: %d",outdeg[i],sz) ;
  30. printf("\n\n") ;
  31. }
  32. return 0 ;
  33. }
  34.  
Success #stdin #stdout 0s 4644KB
stdin
5
8

1 2
1 3
1 5

2 4
2 5
2 1

3 2

5 4
stdout
Number of Nodes: Number of Edges: Node -> Node
Node-1 is connected with: 2 3 5 
Indegree: 1 & Outdegree: 3

Node-2 is connected with: 4 5 1 
Indegree: 2 & Outdegree: 3

Node-3 is connected with: 2 
Indegree: 1 & Outdegree: 1

Node-4 is connected with: 
Indegree: 2 & Outdegree: 0

Node-5 is connected with: 4 
Indegree: 2 & Outdegree: 1