結果

問題 No.556 仁義なきサルたち
ユーザー Mr.FukuMr.Fuku
提出日時 2018-07-19 22:41:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 1,106 bytes
コンパイル時間 205 ms
コンパイル使用メモリ 10,980 KB
実行使用メモリ 9,848 KB
最終ジャッジ日時 2023-08-26 17:37:18
合計ジャッジ時間 2,434 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,872 KB
testcase_01 AC 16 ms
7,872 KB
testcase_02 AC 16 ms
7,880 KB
testcase_03 AC 16 ms
7,868 KB
testcase_04 AC 16 ms
7,876 KB
testcase_05 AC 16 ms
7,876 KB
testcase_06 AC 17 ms
7,932 KB
testcase_07 AC 17 ms
7,916 KB
testcase_08 AC 17 ms
7,868 KB
testcase_09 AC 18 ms
8,232 KB
testcase_10 AC 19 ms
8,236 KB
testcase_11 AC 21 ms
8,372 KB
testcase_12 AC 21 ms
8,292 KB
testcase_13 AC 22 ms
9,008 KB
testcase_14 AC 33 ms
9,044 KB
testcase_15 AC 38 ms
8,880 KB
testcase_16 AC 40 ms
9,644 KB
testcase_17 AC 51 ms
9,552 KB
testcase_18 AC 69 ms
9,632 KB
testcase_19 AC 67 ms
9,848 KB
testcase_20 AC 62 ms
9,684 KB
testcase_21 AC 62 ms
9,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M = map(int,input().split())
mon_list = [[i] for i in range(N+1)]    #N番目のリストにはN番目の猿の所属するチームの情報が書かれたリスト(先頭に所属先ボス、N番目の猿がボスであれば以下に子分)を格納。なお0番目のリストは不使用

for i in range(M):
    mon1,mon2 = map(int,input().split())
    boss1,boss2 = mon_list[mon1][0],mon_list[mon2][0]
    if boss1==boss2:    #同じチームであれば変化なし
        continue
    winner = 0
    loser = 0
    if len(mon_list[boss1])>len(mon_list[boss2]):
        winner,loser = boss1,boss2
    elif len(mon_list[boss2])>len(mon_list[boss1]):
        winner,loser = boss2,boss1
    else:
        winner,loser = min(boss1,boss2),max(boss1,boss2)
    mon_list[winner] += mon_list[loser] #勝ったチームのボスのリストに負けたチームの猿たちを加える
    for i in mon_list[loser]:           #負けたチームの猿たちのリストを勝ったチームのボスに書き換える
        mon_list[i] = [winner]
    
for i in range(1,N+1):
    print(mon_list[i][0])
0