結果

問題 No.1390 Get together
ユーザー 👑 tamatotamato
提出日時 2021-02-12 21:45:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 617 ms / 2,000 ms
コード長 1,207 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 87,724 KB
実行使用メモリ 152,700 KB
最終ジャッジ日時 2023-09-27 03:32:09
合計ジャッジ時間 12,707 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
72,112 KB
testcase_01 AC 94 ms
72,048 KB
testcase_02 AC 94 ms
72,040 KB
testcase_03 AC 156 ms
80,576 KB
testcase_04 AC 143 ms
80,316 KB
testcase_05 AC 150 ms
80,368 KB
testcase_06 AC 145 ms
80,276 KB
testcase_07 AC 143 ms
80,316 KB
testcase_08 AC 143 ms
80,304 KB
testcase_09 AC 152 ms
80,776 KB
testcase_10 AC 96 ms
72,224 KB
testcase_11 AC 94 ms
72,100 KB
testcase_12 AC 96 ms
72,184 KB
testcase_13 AC 94 ms
71,784 KB
testcase_14 AC 95 ms
72,048 KB
testcase_15 AC 93 ms
71,780 KB
testcase_16 AC 397 ms
130,032 KB
testcase_17 AC 379 ms
147,312 KB
testcase_18 AC 408 ms
146,932 KB
testcase_19 AC 591 ms
151,968 KB
testcase_20 AC 588 ms
142,720 KB
testcase_21 AC 574 ms
142,688 KB
testcase_22 AC 560 ms
148,576 KB
testcase_23 AC 559 ms
150,208 KB
testcase_24 AC 542 ms
149,916 KB
testcase_25 AC 617 ms
152,700 KB
testcase_26 AC 617 ms
152,340 KB
testcase_27 AC 610 ms
152,468 KB
testcase_28 AC 599 ms
152,572 KB
testcase_29 AC 584 ms
152,292 KB
testcase_30 AC 617 ms
152,268 KB
testcase_31 AC 598 ms
152,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

    N, M = map(int, input().split())
    seen = [0] * (N+1)
    ans = 0
    box = [set() for _ in range(M+1)]
    for _ in range(N):
        b, c = map(int, input().split())
        box[b].add(c)

    adj = [set() for _ in range(N+1)]
    for b in range(1, M+1):
        C = list(box[b])
        if len(C) > 1:
            c0 = C[0]
            for c in C[1:]:
                adj[c0].add(c)
                adj[c].add(c0)

    root = [-1] * (N+1)
    for v0 in range(1, N+1):
        if seen[v0]:
            continue
        root[v0] = v0
        que = deque()
        que.append(v0)
        seen[1] = 1
        while que:
            v = que.popleft()
            for u in adj[v]:
                if seen[u] == 0:
                    seen[u] = 1
                    que.append(u)
                    root[u] = v0

    cnt = [0] * (N+1)
    for i in range(1, M+1):
        if box[i]:
            b = min(list(box[i]))
            cnt[root[b]] += 1
    for i in range(1, N+1):
        if cnt[i]:
            ans += cnt[i] - 1
    print(ans)


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