結果
問題 | No.2536 同値性と充足可能性 |
ユーザー | nikoro256 |
提出日時 | 2023-11-10 22:27:38 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,798 bytes |
コンパイル時間 | 168 ms |
コンパイル使用メモリ | 82,152 KB |
実行使用メモリ | 116,960 KB |
最終ジャッジ日時 | 2024-09-26 01:51:27 |
合計ジャッジ時間 | 6,787 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
54,528 KB |
testcase_01 | AC | 44 ms
54,144 KB |
testcase_02 | AC | 42 ms
54,528 KB |
testcase_03 | AC | 42 ms
54,400 KB |
testcase_04 | AC | 41 ms
54,528 KB |
testcase_05 | AC | 47 ms
54,528 KB |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | AC | 42 ms
54,316 KB |
testcase_10 | AC | 43 ms
55,260 KB |
testcase_11 | AC | 41 ms
55,052 KB |
testcase_12 | AC | 43 ms
54,984 KB |
testcase_13 | WA | - |
testcase_14 | AC | 43 ms
55,848 KB |
testcase_15 | AC | 42 ms
55,796 KB |
testcase_16 | AC | 44 ms
54,580 KB |
testcase_17 | AC | 79 ms
74,624 KB |
testcase_18 | AC | 76 ms
73,216 KB |
testcase_19 | AC | 44 ms
54,912 KB |
testcase_20 | AC | 83 ms
76,928 KB |
testcase_21 | AC | 93 ms
76,672 KB |
testcase_22 | AC | 94 ms
76,800 KB |
testcase_23 | AC | 201 ms
79,728 KB |
testcase_24 | AC | 191 ms
79,508 KB |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
ソースコード
from collections import defaultdict 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 members_count(self): dic=defaultdict(int) for i in range(self.n): dic[self.find(i)]+=1 return dic 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()) N,M=map(int,input().split()) uf=UnionFind(N) no_flag=False edge=[[] for _ in range(N)] for _ in range(M): i,E,j=map(str,input().split()) i=int(i) j=int(j) i-=1 j-=1 if E=='<==>': uf.union(i,j) else: edge[i].append(j) edge[j].append(i) edge2=[[] for _ in range(N)] for i in range(N): f1=uf.find(i) for e in edge[i]: f2=uf.find(e) if f1==f2: print('No') exit(0) else: edge2[f1].append(f2) edge2[f2].append(f1) edge=edge2 from collections import deque def bfs(s): dq=deque() dq.append([s,0]) global dist while len(dq)!=0: p,h=dq.popleft() for e in edge[p]: if dist[e]==-1: dq.append([e,h+1]) dist[e]=1-dist[p] for d in dic[e]: ans[1-dist[p]].append(d) elif dist[e]!=1-dist[p]: print('No') exit(0) return roots=uf.roots() dist=[-1 for _ in range(N)] dic=uf.all_group_members() ans=[[],[]] for r in roots: if dist[r]==-1: dist[r]=1 for d in dic[r]: ans[1].append(d) bfs(r) print('Yes') if len(ans[0])>len(ans[1]): print(len(ans[0])) print(*[a+1 for a in ans[0]]) else: print(len(ans[1])) print(*[a+1 for a in ans[1]])