結果
問題 | No.2536 同値性と充足可能性 |
ユーザー | rlangevin |
提出日時 | 2023-11-10 21:50:16 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 425 ms / 2,000 ms |
コード長 | 2,242 bytes |
コンパイル時間 | 280 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 119,688 KB |
最終ジャッジ日時 | 2024-09-26 01:23:37 |
合計ジャッジ時間 | 5,361 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
54,400 KB |
testcase_01 | AC | 38 ms
52,608 KB |
testcase_02 | AC | 48 ms
54,016 KB |
testcase_03 | AC | 38 ms
52,480 KB |
testcase_04 | AC | 44 ms
54,016 KB |
testcase_05 | AC | 45 ms
54,400 KB |
testcase_06 | AC | 44 ms
54,400 KB |
testcase_07 | AC | 45 ms
54,144 KB |
testcase_08 | AC | 44 ms
54,016 KB |
testcase_09 | AC | 43 ms
54,016 KB |
testcase_10 | AC | 46 ms
54,144 KB |
testcase_11 | AC | 43 ms
54,400 KB |
testcase_12 | AC | 43 ms
53,888 KB |
testcase_13 | AC | 43 ms
54,016 KB |
testcase_14 | AC | 43 ms
54,144 KB |
testcase_15 | AC | 46 ms
54,400 KB |
testcase_16 | AC | 46 ms
54,400 KB |
testcase_17 | AC | 62 ms
66,304 KB |
testcase_18 | AC | 60 ms
65,408 KB |
testcase_19 | AC | 40 ms
52,480 KB |
testcase_20 | AC | 62 ms
69,376 KB |
testcase_21 | AC | 81 ms
76,416 KB |
testcase_22 | AC | 74 ms
74,240 KB |
testcase_23 | AC | 147 ms
79,872 KB |
testcase_24 | AC | 137 ms
79,360 KB |
testcase_25 | AC | 425 ms
116,588 KB |
testcase_26 | AC | 408 ms
117,832 KB |
testcase_27 | AC | 314 ms
109,708 KB |
testcase_28 | AC | 409 ms
119,688 KB |
testcase_29 | AC | 368 ms
114,084 KB |
testcase_30 | AC | 269 ms
109,952 KB |
ソースコード
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)