fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int n,m,i,j,k,a[101][101];
  5. int main()
  6. {
  7. cin>>n>>m;
  8. for(k=1;k<=m;k++)
  9. {
  10. cin>>i>>j;
  11. a[i][j]=1;
  12. }
  13. for(k=1;k<=n;k++)
  14. for(i=1;i<=n;i++)
  15. for(j=1;j<=n;j++)
  16. if(a[i][j]==0) a[i][j]=a[i][k]*a[k][j];
  17. for(i=1;i<=n;i++)
  18. {
  19. for(j=1;j<=n;j++)
  20. cout<<a[i][j]<<' ';
  21. cout<<'\n';
  22. }
  23. return 0;
  24.  
  25. }
Success #stdin #stdout 0.01s 5268KB
stdin
6 9
1 2
1 3
1 5
3 5
4 1
3 4
5 1
6 1
6 3
stdout
1 1 1 1 1 0 
0 0 0 0 0 0 
1 1 1 1 1 0 
1 1 1 1 1 0 
1 1 1 1 1 0 
1 1 1 1 1 0