結果

問題 No.556 仁義なきサルたち
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-17 23:12:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 700 bytes
コンパイル時間 1,261 ms
コンパイル使用メモリ 86,572 KB
実行使用メモリ 78,116 KB
最終ジャッジ日時 2023-08-20 05:11:15
合計ジャッジ時間 3,970 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
70,992 KB
testcase_01 AC 72 ms
71,148 KB
testcase_02 AC 73 ms
70,920 KB
testcase_03 AC 73 ms
71,132 KB
testcase_04 AC 73 ms
70,984 KB
testcase_05 AC 73 ms
71,324 KB
testcase_06 AC 72 ms
70,916 KB
testcase_07 AC 75 ms
71,184 KB
testcase_08 AC 75 ms
70,936 KB
testcase_09 AC 84 ms
75,260 KB
testcase_10 AC 83 ms
75,676 KB
testcase_11 AC 83 ms
75,368 KB
testcase_12 AC 85 ms
75,520 KB
testcase_13 AC 90 ms
76,128 KB
testcase_14 AC 122 ms
77,604 KB
testcase_15 AC 134 ms
77,508 KB
testcase_16 AC 109 ms
78,116 KB
testcase_17 AC 131 ms
77,548 KB
testcase_18 AC 160 ms
78,116 KB
testcase_19 AC 93 ms
76,628 KB
testcase_20 AC 97 ms
76,740 KB
testcase_21 AC 96 ms
76,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
root = [-1] * N


def find(x):
    if root[x] < 0:
        return x
    root[x] = find(root[x])
    return root[x]


def same(x, y):
    return find(x) == find(y)


def merge(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        return
    if root[x] == root[y]:
        x, y = min(x, y), max(x, y)
        root[x] += root[y]
        root[y] = x
        return
    if -root[x] < -root[y]:
        x, y = y, x
    root[x] += root[y]
    root[y] = x
    return


for _ in range(M):
    a, b = map(int, input().split())
    merge(a - 1, b - 1)
for i in range(N):
    print(find(i) + 1)
0