結果

問題 No.556 仁義なきサルたち
ユーザー sue_charo
提出日時 2017-08-11 23:21:50
言語 Python3
(3.8.1 + numpy 1.14.5 + scipy 1.1.0)
結果
AC  
実行時間 948 ms
コード長 1,635 Byte
コンパイル時間 67 ms
使用メモリ 11,860 KB
最終ジャッジ日時 2019-12-29 01:13:13

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
test1.txt AC 20 ms
5,620 KB
test2.txt AC 19 ms
5,620 KB
test3.txt AC 18 ms
5,624 KB
test4.txt AC 19 ms
5,620 KB
test5.txt AC 17 ms
5,620 KB
test6.txt AC 18 ms
5,632 KB
test7.txt AC 19 ms
5,640 KB
test8.txt AC 19 ms
5,640 KB
test9.txt AC 20 ms
5,644 KB
test10.txt AC 19 ms
5,944 KB
test11.txt AC 21 ms
5,956 KB
test12.txt AC 25 ms
5,952 KB
test13.txt AC 24 ms
6,088 KB
test14.txt AC 23 ms
7,520 KB
test15.txt AC 34 ms
8,028 KB
test16.txt AC 77 ms
8,424 KB
test17.txt AC 38 ms
10,292 KB
test18.txt AC 53 ms
10,916 KB
test19.txt AC 301 ms
11,728 KB
x_append10.txt AC 948 ms
11,540 KB
x_append11.txt AC 441 ms
11,860 KB
x_append12.txt AC 454 ms
11,604 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