結果

問題 No.556 仁義なきサルたち
ユーザー 37zigen37zigen
提出日時 2018-04-15 00:03:16
言語 Fortran
(gFortran 13.2.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 1,305 bytes
コンパイル時間 1,055 ms
コンパイル使用メモリ 28,360 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-09 05:30:38
合計ジャッジ時間 2,397 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 6 ms
4,380 KB
testcase_17 AC 7 ms
4,376 KB
testcase_18 AC 10 ms
4,380 KB
testcase_19 AC 9 ms
4,380 KB
testcase_20 AC 9 ms
4,376 KB
testcase_21 AC 10 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

module md
contains
end module
module djset_md
    type DJSet
        integer::n
        integer,allocatable::upper(:)
    end type

contains
    subroutine build(o,n_)
        type(DJSet)::o
        integer::n_
        o%n=n_
        allocate(o%upper(o%n))
        do i=1,o%n
            o%upper(i)=-1
        end do
    end subroutine

    recursive function root(o,x)result(res)
        implicit none
        type(DJSet)::o
        integer::x,res
        if(o%upper(x)<0)then
            res=x
        else
            o%upper(x)=root(o,o%upper(x))
            res=o%upper(x)
        end if
    end function

    subroutine setUnion(o,x,y)
        integer::x,y
        type(DJSet)::o
        x=root(o,x)
        y=root(o,y)
        if(x==y)return
        if(o%upper(x)<o%upper(y).or.(o%upper(x)==o%upper(y).and.x<y))then
            x=xor(x,y)
            y=xor(x,y)
            x=xor(x,y)
        end if
        !yの方が強い集団
        o%upper(y)=o%upper(x)+o%upper(y)
        o%upper(x)=y
    end subroutine



end module

program main
    use djset_md
    implicit none
    integer::n,m,i,j,k,a,b
    type(DJSet)::ds
    read(*,*)n,m
    call build(ds,n)
    do i=1,m
        read(*,*)a,b
        call setUnion(ds,a,b)
    end do
    do i=1,n
        print*,root(ds,i)
    end do
end program
0