結果
問題 | No.2536 同値性と充足可能性 |
ユーザー | nikoro256 |
提出日時 | 2023-11-10 22:31:58 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 443 ms / 2,000 ms |
コード長 | 2,814 bytes |
コンパイル時間 | 346 ms |
コンパイル使用メモリ | 82,164 KB |
実行使用メモリ | 116,992 KB |
最終ジャッジ日時 | 2024-09-26 01:54:34 |
合計ジャッジ時間 | 6,104 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 46 ms
54,272 KB |
testcase_01 | AC | 47 ms
54,016 KB |
testcase_02 | AC | 47 ms
54,528 KB |
testcase_03 | AC | 47 ms
54,144 KB |
testcase_04 | AC | 46 ms
54,528 KB |
testcase_05 | AC | 47 ms
54,400 KB |
testcase_06 | AC | 46 ms
54,400 KB |
testcase_07 | AC | 46 ms
54,400 KB |
testcase_08 | AC | 46 ms
54,528 KB |
testcase_09 | AC | 46 ms
54,400 KB |
testcase_10 | AC | 45 ms
54,400 KB |
testcase_11 | AC | 46 ms
54,528 KB |
testcase_12 | AC | 46 ms
54,400 KB |
testcase_13 | AC | 46 ms
54,272 KB |
testcase_14 | AC | 46 ms
54,272 KB |
testcase_15 | AC | 47 ms
54,144 KB |
testcase_16 | AC | 47 ms
54,144 KB |
testcase_17 | AC | 90 ms
74,752 KB |
testcase_18 | AC | 85 ms
73,088 KB |
testcase_19 | AC | 48 ms
55,040 KB |
testcase_20 | AC | 98 ms
76,928 KB |
testcase_21 | AC | 106 ms
76,544 KB |
testcase_22 | AC | 110 ms
76,672 KB |
testcase_23 | AC | 221 ms
79,744 KB |
testcase_24 | AC | 213 ms
79,232 KB |
testcase_25 | AC | 428 ms
106,368 KB |
testcase_26 | AC | 443 ms
112,896 KB |
testcase_27 | AC | 330 ms
103,276 KB |
testcase_28 | AC | 414 ms
116,992 KB |
testcase_29 | AC | 396 ms
111,488 KB |
testcase_30 | AC | 295 ms
105,044 KB |
ソースコード
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(*sorted([a+1 for a in ans[0]])) else: print(len(ans[1])) print(*sorted([a+1 for a in ans[1]]))