
program grdeg

    integer, allocatable :: A(:,:), B(:,:), ecc(:)
    logical :: directed 
    
    read(*, *) directed, n
    print*, directed, n
    allocate(A(n,n), B(n,n), ecc(n))

    call rdmat(A, n, directed )
    	!call prmat (A, n, 2)
    call connect(A, n, B) 
    
    ecc = maxval(B, 1)
    irad =  minval(ecc, ecc > 0)
    if (directed) then 
        print*, 'directed graph'
    else
        print*, 'undirected graph'
    end if
    
1 format(A14, (T15, 4(I4:, ',')))    
    print 1, 'radius:        ',irad
    print 1, 'diameter:      ', maxval(ecc)
    print 1, 'center points: ', pack(  (/ (i, i = 1,n) /), ecc == irad)
    
   ! call prmat(B, n, 2)

contains

    subroutine prmat(A, n, d)
        character fmt*40
        integer, intent(in) :: n, d, A(n,n)
        write(fmt, '(A,I0,A,I0,A)') '(', n, '(I', d, ':,X))'
        write(*, fmt) ((A(j,i), j=1,n) ,i=1,n)
    end subroutine
    
    subroutine rdmat(A, n, directed)
        integer, intent(out) :: A(n,n)
        logical directed
        A = 0
        do
            read(*,*, end=1) i,j
            A(i,j) = A(i,j)+1
            if (.not. directed)  A(j,i) = A(j,i)+1
        end do
    1   continue  
    end subroutine
    
    subroutine connect(A, n,  B)
        integer, intent(in) :: A(n,n)
        integer, intent(out):: B(n,n)
        integer:: C(n,n)

        B = 0
        C = A
    
        do i=1,n
            where (B == 0 .and. C > 0) B = i
            C = MATMUL(A,C)
        end do
        
        do i=1,n
            B(i,i) = 0
        end do
        
    end subroutine

end program

