結果

問題 No.2536 同値性と充足可能性
ユーザー mo124121mo124121
提出日時 2023-11-10 22:19:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 436 ms / 2,000 ms
コード長 2,128 bytes
コンパイル時間 141 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 99,644 KB
最終ジャッジ日時 2023-11-10 22:19:33
合計ジャッジ時間 5,918 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,732 KB
testcase_01 AC 39 ms
55,732 KB
testcase_02 AC 40 ms
55,732 KB
testcase_03 AC 39 ms
55,732 KB
testcase_04 AC 40 ms
55,732 KB
testcase_05 AC 43 ms
55,732 KB
testcase_06 AC 39 ms
55,732 KB
testcase_07 AC 40 ms
55,732 KB
testcase_08 AC 44 ms
55,732 KB
testcase_09 AC 40 ms
55,732 KB
testcase_10 AC 40 ms
55,732 KB
testcase_11 AC 40 ms
55,732 KB
testcase_12 AC 43 ms
55,732 KB
testcase_13 AC 39 ms
55,732 KB
testcase_14 AC 40 ms
55,732 KB
testcase_15 AC 39 ms
55,732 KB
testcase_16 AC 40 ms
55,732 KB
testcase_17 AC 63 ms
75,952 KB
testcase_18 AC 63 ms
75,952 KB
testcase_19 AC 40 ms
55,732 KB
testcase_20 AC 80 ms
76,204 KB
testcase_21 AC 83 ms
76,228 KB
testcase_22 AC 83 ms
76,228 KB
testcase_23 AC 210 ms
76,728 KB
testcase_24 AC 236 ms
76,604 KB
testcase_25 AC 385 ms
97,468 KB
testcase_26 AC 390 ms
96,956 KB
testcase_27 AC 436 ms
97,468 KB
testcase_28 AC 374 ms
98,236 KB
testcase_29 AC 391 ms
97,724 KB
testcase_30 AC 385 ms
99,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(2 * 10**3)

if sys.implementation.name == "pypy":
    import pypyjit

    pypyjit.set_param("max_unroll_recursion=-1")


from collections import defaultdict


input = lambda: sys.stdin.readline().rstrip()


class UnionFind:
    def __init__(self, N):
        self.N = N
        self.parent_or_size = [-1] * N
        self.group_count = N

    def root(self, x):
        if self.parent_or_size[x] < 0:
            return x
        else:
            self.parent_or_size[x] = self.root(self.parent_or_size[x])
            return self.parent_or_size[x]

    def unite(self, x, y):
        root_x = self.root(x)
        root_y = self.root(y)
        if root_x == root_y:
            return
        if -self.parent_or_size[root_x] < -self.parent_or_size[root_y]:
            root_x, root_y = root_y, root_x
        self.parent_or_size[root_x] += self.parent_or_size[root_y]
        self.parent_or_size[root_y] = root_x
        self.group_count -= 1

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.parent_or_size[self.root(x)]

    def groups(self):
        ret = defaultdict(list)
        for i in range(self.N):
            ret[self.root(i)].append(i)
        return [group for group in ret.values()]


N, M = map(int, input().split())

uf = UnionFind(N * 2)
related = UnionFind(N)
for _ in range(M):
    i, s, j = input().split()
    i = int(i) - 1
    j = int(j) - 1
    if len(s) == 4:
        uf.unite(i, j)
        uf.unite(i + N, j + N)
    else:
        uf.unite(i, j + N)
        uf.unite(i + N, j)
    related.unite(i, j)

for i in range(N):
    if uf.same(i, i + N):
        print("No")
        exit()

ans = []
for grp in related.groups():
    cand1 = []
    cand2 = []
    f = grp[0]
    for node in grp:
        if uf.same(f, node):
            cand1.append(node + 1)
        else:
            cand2.append(node + 1)
    if len(cand1) > len(cand2):
        ans.extend(cand1)
    else:
        ans.extend(cand2)

if len(ans) >= N // 2:
    print("Yes")
    print(len(ans))
    print(*sorted(ans))
else:
    print("No")
0