結果

問題 No.556 仁義なきサルたち
ユーザー Mr.FukuMr.Fuku
提出日時 2018-08-02 10:16:41
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 671 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 11,964 KB
実行使用メモリ 11,496 KB
最終ジャッジ日時 2023-10-19 21:00:09
合計ジャッジ時間 2,244 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
10,160 KB
testcase_01 AC 31 ms
10,160 KB
testcase_02 AC 31 ms
10,160 KB
testcase_03 AC 30 ms
10,160 KB
testcase_04 AC 29 ms
10,160 KB
testcase_05 AC 29 ms
10,184 KB
testcase_06 AC 30 ms
10,184 KB
testcase_07 AC 30 ms
10,184 KB
testcase_08 AC 30 ms
10,184 KB
testcase_09 AC 32 ms
10,344 KB
testcase_10 AC 33 ms
10,344 KB
testcase_11 AC 35 ms
10,356 KB
testcase_12 AC 34 ms
10,356 KB
testcase_13 AC 37 ms
10,904 KB
testcase_14 AC 47 ms
10,908 KB
testcase_15 AC 51 ms
10,912 KB
testcase_16 AC 53 ms
11,496 KB
testcase_17 AC 65 ms
11,496 KB
testcase_18 AC 86 ms
11,496 KB
testcase_19 AC 80 ms
11,388 KB
testcase_20 AC 80 ms
11,396 KB
testcase_21 AC 80 ms
11,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M = map(int,input().split())
apes = [[i,1]for i in range(N+1)]

def bossfind(ape):
    if apes[ape][0]==ape:
        return ape
    else:
        apes[ape][0] = bossfind(apes[ape][0])
        return apes[ape][0]

for i in range(M):
    ape1,ape2 = map(int,input().split())
    boss1,boss2 = bossfind(ape1),bossfind(ape2)
    if boss1==boss2:
        continue
    elif apes[boss1][1]==apes[boss2][1]:
        win,lose = min(boss1,boss2),max(boss1,boss2)
    elif apes[boss1][1]>apes[boss2][1]:
        win,lose = boss1,boss2
    else:
        win,lose = boss2,boss1
    apes[win][1] += apes[lose][1]
    apes[lose][0] = win

for i in range(1,N+1):
    print(bossfind(i))
0