結果

問題 No.2536 同値性と充足可能性
ユーザー rlangevinrlangevin
提出日時 2023-11-10 21:50:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 515 ms / 2,000 ms
コード長 2,242 bytes
コンパイル時間 265 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 119,396 KB
最終ジャッジ日時 2023-11-10 21:50:22
合計ジャッジ時間 6,108 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,740 KB
testcase_01 AC 38 ms
53,588 KB
testcase_02 AC 44 ms
55,740 KB
testcase_03 AC 38 ms
53,588 KB
testcase_04 AC 43 ms
55,740 KB
testcase_05 AC 42 ms
55,740 KB
testcase_06 AC 42 ms
55,740 KB
testcase_07 AC 42 ms
55,740 KB
testcase_08 AC 43 ms
55,740 KB
testcase_09 AC 42 ms
55,740 KB
testcase_10 AC 42 ms
55,740 KB
testcase_11 AC 42 ms
55,740 KB
testcase_12 AC 43 ms
55,740 KB
testcase_13 AC 43 ms
55,740 KB
testcase_14 AC 44 ms
55,740 KB
testcase_15 AC 43 ms
55,740 KB
testcase_16 AC 43 ms
55,740 KB
testcase_17 AC 58 ms
66,504 KB
testcase_18 AC 55 ms
66,504 KB
testcase_19 AC 39 ms
53,588 KB
testcase_20 AC 57 ms
70,728 KB
testcase_21 AC 75 ms
75,900 KB
testcase_22 AC 66 ms
74,240 KB
testcase_23 AC 148 ms
79,604 KB
testcase_24 AC 140 ms
79,496 KB
testcase_25 AC 515 ms
115,792 KB
testcase_26 AC 456 ms
116,908 KB
testcase_27 AC 335 ms
108,796 KB
testcase_28 AC 477 ms
119,396 KB
testcase_29 AC 401 ms
113,308 KB
testcase_30 AC 293 ms
109,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]


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

U = UnionFind(N)
Edge = []
for _ in range(M):
    i, e, j = input().split()
    i, j = int(i) - 1, int(j) - 1
    if e == "<==>":
        U.union(i, j)
    else:
        Edge.append((i, j))

G = [set() for i in range(N)]    
for i, j in Edge:
    if U.is_same(i, j):
        print("No")
        exit()
    ui, uj = U.find(i), U.find(j)
    G[ui].add(uj)
    G[uj].add(ui)
        
SS = [set() for i in range(N)]
for i in range(N):
    SS[U.find(i)].add(i)
    
seen = [-1] * N
from collections import *
ans = []
for i in range(N):
    u = U.find(i)
    if seen[u] != -1:
        continue
    Q = deque([u])
    one, zero = [], []
    seen[u] = 1
    one.append(u)
    while Q:
        u = Q.popleft()
        for v in G[u]:
            if seen[v] != -1:
                if seen[v] != 1 - seen[u]:
                    print("No")
                    exit()
                continue
            seen[v] = 1 - seen[u]
            if seen[v]:
                one.append(v)
            else:
                zero.append(v)
            Q.append(v)
    Lo, Lz = [], []
    for i in one:
        for s in SS[i]:
            Lo.append(s + 1)
    for i in zero:
        for s in SS[i]:
            Lz.append(s + 1)
    if len(Lz) >= len(Lo):
        ans.extend(Lz)
    else:
        ans.extend(Lo)
        
ans.sort()
print("Yes")
print(len(ans))
print(*ans)
0