結果

問題 No.556 仁義なきサルたち
ユーザー Pump0129Pump0129
提出日時 2018-05-24 00:34:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,296 bytes
コンパイル時間 813 ms
コンパイル使用メモリ 10,920 KB
実行使用メモリ 14,896 KB
最終ジャッジ日時 2023-09-11 02:06:02
合計ジャッジ時間 10,520 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
14,896 KB
testcase_01 AC 16 ms
7,852 KB
testcase_02 AC 16 ms
7,940 KB
testcase_03 AC 16 ms
7,756 KB
testcase_04 AC 16 ms
7,856 KB
testcase_05 AC 17 ms
8,288 KB
testcase_06 AC 17 ms
8,272 KB
testcase_07 AC 18 ms
8,136 KB
testcase_08 AC 18 ms
8,124 KB
testcase_09 AC 31 ms
8,188 KB
testcase_10 AC 47 ms
8,252 KB
testcase_11 AC 56 ms
8,292 KB
testcase_12 AC 59 ms
8,212 KB
testcase_13 AC 87 ms
9,072 KB
testcase_14 AC 634 ms
9,268 KB
testcase_15 AC 820 ms
9,256 KB
testcase_16 AC 1,306 ms
9,900 KB
testcase_17 TLE -
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

def get_root(monkey_data, monkey_id):
    for monkey_root in monkey_data:
        if monkey_id in monkey_data[monkey_root]:
            return monkey_root


if __name__ == "__main__":
    n, m = map(int, input().split(" "))
    root_data = {i + 1: (i + 1,) for i in range(n)}
    for i in range(m):
        a, b = map(int, input().split(" "))
        a_root = get_root(root_data, a)
        b_root = get_root(root_data, b)
        if a_root != b_root:
            if len(root_data[a_root]) != len(root_data[b_root]):
                if len(root_data[a_root]) < len(root_data[b_root]):
                    root_data.update({b_root: root_data[b_root] + root_data.pop(a_root)})
                else:
                    root_data.update({a_root: root_data[a_root] + root_data.pop(b_root)})
            else:
                if a_root < b_root:
                    root_data.update({a_root: root_data[a_root] + root_data.pop(b_root)})
                else:
                    root_data.update({b_root: root_data[b_root] + root_data.pop(a_root)})
    monkey_root_list = [0 for i in range(n)]
    for monkey_key in root_data:
        for monkey_num in root_data[monkey_key]:
            monkey_root_list[monkey_num - 1] = monkey_key
    print(*monkey_root_list, sep="\n")
0