結果

問題 No.556 仁義なきサルたち
ユーザー sue_charosue_charo
提出日時 2017-08-11 23:21:50
言語 Python3
(3.8.3 + numpy 1.14.5 + scipy 1.1.0)
結果
AC  
実行時間 904 ms / 2,000 ms
コード長 1,635 Byte
コンパイル時間 53 ms
使用メモリ 14,068 KB
最終ジャッジ日時 2020-08-20 06:48:46
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 15 ms
8,188 KB
testcase_01 AC 15 ms
8,260 KB
testcase_02 AC 15 ms
8,264 KB
testcase_03 AC 16 ms
8,176 KB
testcase_04 AC 15 ms
8,160 KB
testcase_05 AC 16 ms
8,184 KB
testcase_06 AC 17 ms
8,304 KB
testcase_07 AC 18 ms
8,156 KB
testcase_08 AC 17 ms
8,156 KB
testcase_09 AC 17 ms
8,324 KB
testcase_10 AC 18 ms
8,568 KB
testcase_11 AC 22 ms
8,628 KB
testcase_12 AC 22 ms
8,608 KB
testcase_13 AC 20 ms
10,108 KB
testcase_14 AC 29 ms
10,512 KB
testcase_15 AC 73 ms
10,788 KB
testcase_16 AC 34 ms
12,432 KB
testcase_17 AC 48 ms
13,064 KB
testcase_18 AC 292 ms
13,884 KB
testcase_19 AC 904 ms
13,616 KB
testcase_20 AC 447 ms
13,960 KB
testcase_21 AC 465 ms
14,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #
# coding: utf-8
def II(): return int(input())
def ILI(): return list(map(int, input().split()))


def read():
    N, M = ILI()
    A_B = [ILI() for __ in range(M)]
    return N, M, A_B


def solve(N, M, A_B):
    n_boss = list(range(N + 1))
    d_team = dict()
    for i in range(1, N + 1):
        d_team[i] = {i}

    for a, b in A_B:
        a_boss = n_boss[a]
        b_boss = n_boss[b]
        a_team_num = len(d_team[a_boss])
        b_team_num = len(d_team[b_boss])
        if a_boss == b_boss:
            continue
        else:
            if a_team_num == b_team_num:
                if a_boss <= b_boss:
                    d_team[a_boss] = d_team[a_boss] | d_team[b_boss]
                    for ele in d_team[b_boss]:
                        n_boss[ele] = a_boss
                    d_team[b_boss] = None
                else:
                    d_team[b_boss] = d_team[a_boss] | d_team[b_boss]
                    for ele in d_team[a_boss]:
                        n_boss[ele] = b_boss
                    d_team[a_boss] = None
            elif a_team_num > b_team_num:
                d_team[a_boss] = d_team[a_boss] | d_team[b_boss]
                for ele in d_team[b_boss]:
                    n_boss[ele] = a_boss
                d_team[b_boss] = None
            else:
                d_team[b_boss] = d_team[a_boss] | d_team[b_boss]
                for ele in d_team[a_boss]:
                    n_boss[ele] = b_boss
                d_team[a_boss] = None

    ans = "\n".join(map(str, n_boss[1:]))
    return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()
0