結果

問題 No.2536 同値性と充足可能性
ユーザー yupoohyupooh
提出日時 2023-11-10 22:32:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 768 ms / 2,000 ms
コード長 2,536 bytes
コンパイル時間 245 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 133,600 KB
最終ジャッジ日時 2023-11-10 22:32:42
合計ジャッジ時間 7,839 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,740 KB
testcase_01 AC 44 ms
55,740 KB
testcase_02 AC 44 ms
55,740 KB
testcase_03 AC 44 ms
55,740 KB
testcase_04 AC 43 ms
55,740 KB
testcase_05 AC 44 ms
55,740 KB
testcase_06 AC 44 ms
55,740 KB
testcase_07 AC 43 ms
55,740 KB
testcase_08 AC 44 ms
55,740 KB
testcase_09 AC 43 ms
55,740 KB
testcase_10 AC 43 ms
55,740 KB
testcase_11 AC 43 ms
55,740 KB
testcase_12 AC 44 ms
55,740 KB
testcase_13 AC 43 ms
55,740 KB
testcase_14 AC 43 ms
55,740 KB
testcase_15 AC 44 ms
55,740 KB
testcase_16 AC 44 ms
55,740 KB
testcase_17 AC 64 ms
70,808 KB
testcase_18 AC 64 ms
70,808 KB
testcase_19 AC 44 ms
55,740 KB
testcase_20 AC 76 ms
76,304 KB
testcase_21 AC 88 ms
76,672 KB
testcase_22 AC 91 ms
76,676 KB
testcase_23 AC 355 ms
86,568 KB
testcase_24 AC 303 ms
87,700 KB
testcase_25 AC 768 ms
133,600 KB
testcase_26 AC 717 ms
124,896 KB
testcase_27 AC 593 ms
130,016 KB
testcase_28 AC 669 ms
124,128 KB
testcase_29 AC 678 ms
126,560 KB
testcase_30 AC 481 ms
130,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
n,m=map(int,input().split())
adj=[set() for _ in range(n)]
adj2=[set() for _ in range(n)]
from collections import defaultdict,deque

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

uf=UnionFind(n)
for _ in range(m):
  l=list(map(str,input().split()))
  a=int(l[0])-1
  b=int(l[2])-1
  if l[1]=="<==>":
    adj[a].add(b)
    adj[b].add(a)
  else:
    adj2[a].add(b)
    adj2[b].add(a)
  uf.union(a,b)
for i in range(n):
  for j in adj[i]:
    if j in adj2[i]:
      print("No")
      exit()
dist = [-1]*n
for i in range(n):
  if dist[i]!=-1:
    continue
  que = deque([i])
  dist[i] = 0
  while que:
    v = que.popleft()
    e = dist[v]
    for w in adj[v]:
      if dist[w] > -1:
        if dist[w]==1-e:
          print("No")
          exit()
      else:
        dist[w] = e
        que.append(w)
    for w in adj2[v]:
      if dist[w] > -1:
        if dist[w]==e:
          print("No")
          exit()
      else:
        dist[w] = 1-e
        que.append(w)  
ans=[]
for l in list(uf.all_group_members().values()):
  zero=[]
  one=[]
  for i in l:
    if dist[i]==0:
      zero.append(i+1)
    else:
      one.append(i+1)
  if len(zero)>=len(one):
    ans+=zero
  else:
    ans+=one
if len(ans)>=(n+1)//2:
  print("Yes")
  print(len(ans))
  ans.sort()
  print(*ans)
else:
  print("No")
0