結果

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

テストケース

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

ソースコード

diff #

module md
contains
end module
module djset_md
    private
    type,public::DJSet
        integer::n
        integer,allocatable::upper(:)
    contains
        procedure::build=>build_
        procedure::root=>root_
        procedure::setUnion=>setUnion_
    end type

contains
    subroutine build_(o,n_)
        implicit none
        integer::i,j,k
        class(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
        class(DJSet)::o
        integer::x,res
        if(o%upper(x)<0)then
            res=x
        else
            o%upper(x)=o%root(o%upper(x))
            res=o%upper(x)
        end if
    end function

    subroutine setUnion_(o,x,y)
        implicit none
        class(DJSet)::o
        integer::x,y
        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
        o%upper(y)=o%upper(x)+o%upper(y)
        o%upper(x)=y
    end subroutine

    subroutine test
        print*,"fuck"
    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 ds%build(n)
    do i=1,m
        read(*,*)a,b
        call ds%setUnion(a,b)
    end do
    do i=1,n
        print*,ds%root(i)
    end do
end program
0