結果

問題 No.2202 贅沢てりたまチキン
ユーザー meruuu61779999meruuu61779999
提出日時 2023-02-03 21:40:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 738 ms / 2,000 ms
コード長 3,125 bytes
コンパイル時間 448 ms
コンパイル使用メモリ 87,216 KB
実行使用メモリ 87,192 KB
最終ジャッジ日時 2023-09-15 17:19:31
合計ジャッジ時間 6,560 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,520 KB
testcase_01 AC 93 ms
71,644 KB
testcase_02 AC 88 ms
71,532 KB
testcase_03 AC 94 ms
71,564 KB
testcase_04 AC 98 ms
71,840 KB
testcase_05 AC 90 ms
71,728 KB
testcase_06 AC 95 ms
71,752 KB
testcase_07 AC 90 ms
71,564 KB
testcase_08 AC 94 ms
71,408 KB
testcase_09 AC 91 ms
71,696 KB
testcase_10 AC 102 ms
79,284 KB
testcase_11 AC 95 ms
71,608 KB
testcase_12 AC 90 ms
71,544 KB
testcase_13 AC 91 ms
71,716 KB
testcase_14 AC 164 ms
81,104 KB
testcase_15 AC 161 ms
80,864 KB
testcase_16 AC 150 ms
81,204 KB
testcase_17 AC 165 ms
80,960 KB
testcase_18 AC 137 ms
79,228 KB
testcase_19 AC 131 ms
81,080 KB
testcase_20 AC 196 ms
81,440 KB
testcase_21 AC 192 ms
81,400 KB
testcase_22 AC 162 ms
78,184 KB
testcase_23 AC 302 ms
80,244 KB
testcase_24 AC 233 ms
80,172 KB
testcase_25 AC 738 ms
87,192 KB
testcase_26 AC 176 ms
81,392 KB
testcase_27 AC 163 ms
80,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

inputs = sys.stdin.readline


class dsu():
    """Union-Find

    syakayamiさん作、PythonバージョンのACLよりコピペしたものです。
    使わせていただきありがとうございます!
    https://github.com/shakayami/ACL-for-python/blob/master/dsu.py

    ・使い方(個人的まとめ):
        uf=dsu(N): 初期化(Nは頂点の数)
        uf.merge(a,b): 頂点aがある連結成分と頂点bがある連結成分を合体します。
        uf.same(a,b): 頂点a,bが同じ連結成分ならTrue, そうでないならFalseを返します。
        uf.leader(a): 頂点aの連結成分の代表元を返します。
        uf.size(a): 頂点aの連結成分にある超点数を返します(頂点a自身を含みます)。
        uf.groups(): グラフの連結成分の情報を答えます。

    ・使い方URL
        https://github.com/shakayami/ACL-for-python/wiki/dsu
    """
    n = 1
    parent_or_size = [-1 for i in range(n)]

    def __init__(self, N):
        self.n = N
        self.parent_or_size = [-1 for i in range(N)]

    def merge(self, a, b):
        assert 0 <= a < self.n, "0<=a<n,a={0},n={1}".format(a, self.n)
        assert 0 <= b < self.n, "0<=b<n,b={0},n={1}".format(b, self.n)
        x = self.leader(a)
        y = self.leader(b)
        if x == y:
            return x
        if (-self.parent_or_size[x] < -self.parent_or_size[y]):
            x, y = y, x
        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x
        return x

    def same(self, a, b):
        assert 0 <= a < self.n, "0<=a<n,a={0},n={1}".format(a, self.n)
        assert 0 <= b < self.n, "0<=b<n,b={0},n={1}".format(b, self.n)
        return self.leader(a) == self.leader(b)

    def leader(self, a):
        assert 0 <= a < self.n, "0<=a<n,a={0},n={1}".format(a, self.n)
        if (self.parent_or_size[a] < 0):
            return a
        self.parent_or_size[a] = self.leader(self.parent_or_size[a])
        return self.parent_or_size[a]

    def size(self, a):
        assert 0 <= a < self.n, "0<=a<n,a={0},n={1}".format(a, self.n)
        return -self.parent_or_size[self.leader(a)]

    def groups(self):
        leader_buf = [0 for i in range(self.n)]
        group_size = [0 for i in range(self.n)]
        for i in range(self.n):
            leader_buf[i] = self.leader(i)
            group_size[leader_buf[i]] += 1
        result = [[] for i in range(self.n)]
        for i in range(self.n):
            result[leader_buf[i]].append(i)
        result2 = []
        for i in range(self.n):
            if len(result[i]) > 0:
                result2.append(result[i])
        return result2


def main():
    N, M = map(int, inputs().split())
    uf = dsu(N * 2)
    for _ in range(M):
        a, b = map(int, inputs().split())
        a, b = a - 1, b - 1
        a2, b2 = a + N, b + N
        uf.merge(a, b2)
        uf.merge(b, a2)

    for i in range(N):
        if not uf.same(i, i + N):
            print('No')
            return
    print('Yes')


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