結果

問題 No.2536 同値性と充足可能性
ユーザー mo124121
提出日時 2023-11-10 22:02:46
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,785 bytes
コンパイル時間 456 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 84,608 KB
最終ジャッジ日時 2024-09-26 01:33:28
合計ジャッジ時間 5,961 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 WA * 17
権限があれば一括ダウンロードができます

ソースコード

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)
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)

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

ans = [1]
for i in range(1, N):
    if uf.same(0, i):
        ans.append(i + 1)

print("Yes")
print(len(ans))
print(*ans)
0