module graph_mod
    implicit none
    type node
        integer(4),allocatable:: to(:)
        integer(4):: l
    end type
    
contains
    subroutine init(n)
        type(node):: n
        allocate(n%to(1))

        n%l = 0
    end subroutine


    subroutine regist(n,m)
        type(node):: n
        integer(4):: m

        if (n%l+1 > size(n%to)) call add_(n)
        n%l=n%l+1
        n%to(n%l) = m
    end subroutine


    subroutine add_(n)
        type(node):: n
        integer(4),allocatable:: tmp(:)
        integer(4):: l

        l = size(n%to)
        allocate(tmp(l))
        tmp(:) = n%to(:)
        deallocate(n%to)
        allocate(n%to(l*2))
        n%to(1:l) = tmp(:)
        deallocate(tmp)
    end subroutine
end module

module deque_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    type deque
        integer(int32),pointer:: v(:)
        integer(int32):: l,r
        integer(int32):: lmax, rmax
    end type
    private
    public:: deque
    public:: init_deque
    public:: append, appendleft
    public:: pop, popleft
    public:: right, left
    public:: remaining_elements
    public:: remain

contains
    subroutine init_deque(dq)
        ! 初期化
        type(deque):: dq
        dq%r=0; dq%rmax=1
        dq%l=1; dq%lmax=-1
        allocate(dq%v(dq%lmax:dq%rmax))
    end subroutine


    subroutine append(dq,num)
        ! 右端に挿入
        type(deque):: dq
        integer(int32):: num
        if (dq%r+1 > dq%rmax) call add_(dq)
        dq%r=dq%r+1
        dq%v(dq%r) = num
    end subroutine

    subroutine appendleft(dq,num)
        ! 左端に挿入
        type(deque):: dq
        integer(int32):: num
        if (dq%l-1 < dq%lmax) call add_(dq)
        dq%l=dq%l-1
        dq%v(dq%l) = num
    end subroutine

    subroutine add_(dq)
        ! arrayの延長
        type(deque):: dq
        integer(int32):: l
        integer(int32),pointer:: tmp(:)
        l = size(dq%v)
        allocate(tmp(l))
        tmp(:) = dq%v(:)
        deallocate(dq%v)
        allocate(dq%v(2*dq%lmax:2*dq%rmax))
        dq%v(dq%lmax:dq%rmax) = tmp(:)
        dq%lmax = 2*dq%lmax
        dq%rmax = 2*dq%rmax
    end subroutine


    function pop(dq) result(ret)
        ! 右端から取り出し
        type(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%r)
        dq%r=dq%r-1
    end function

    function popleft(dq) result(ret)
        ! 左端から取り出し
        type(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%l)
        dq%l=dq%l+1
    end function


    function right(dq) result(ret)
        ! 右端を確認
        type(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%r)
    end function

    function left(dq) result(ret)
        ! 左端を確認
        type(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%l)
    end function

    function remaining_elements(dq) result(ret)
        ! 残りの要素数
        type(deque):: dq
        integer(int32):: ret 
        ret = dq%r - dq%l + 1
    end function

    function remain(dq) result(ret)
        ! 要素が残っているかどうか
        type(deque):: dq
        logical:: ret
        ret = remaining_elements(dq) > 0
    end function
end module

program main
    use,intrinsic :: iso_fortran_env
    use graph_mod
    use deque_mod
    implicit none
    integer(int32):: n,m,i,j,next,dst
    integer(int32), allocatable:: dist(:), from(:)
    integer(int32), allocatable:: a(:),b(:)
    type(node),allocatable:: g(:)
    type(deque):: task

    read*, n,m
    allocate(a(m),b(m),g(n),dist(n),from(n))
    do i=1,n
        call init(g(i))
    end do

    do i=1,m
        read*, a(i), b(i)
        call regist(g(a(i)),b(i))
        call regist(g(b(i)),a(i))
    end do
    call init_deque(task)
    call append(task,1)
    dist(:) = -1
    dist(1) = 0
    from(:) = -1

    do while (remain(task))
        next = popleft(task)
        do i=1,g(next)%l
            dst = g(next)%to(i)
            if (dist(dst) == -1 .or. dist(dst) > dist(next)+1) then
                call append(task, g(next)%to(i))
                dist(dst) = dist(next)+1
                from(dst) = next
            end if
        end do
    end do

    do i=1,n
        if (dist(i) == -1) then
            print'(a)', 'No'
            stop
        end if
    end do
    
    print'(a)', 'Yes'
    do i=2,n
        print'(i0)', from(i)
    end do

end program main

Measured memory_bytes: 18544