結果
問題 | No.2536 同値性と充足可能性 |
ユーザー | nikoro256 |
提出日時 | 2023-11-10 22:05:35 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,646 bytes |
コンパイル時間 | 256 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 106,892 KB |
最終ジャッジ日時 | 2024-09-26 01:36:14 |
合計ジャッジ時間 | 7,193 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
54,656 KB |
testcase_01 | AC | 45 ms
54,400 KB |
testcase_02 | AC | 45 ms
54,144 KB |
testcase_03 | AC | 44 ms
54,400 KB |
testcase_04 | AC | 47 ms
54,144 KB |
testcase_05 | AC | 47 ms
54,400 KB |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | AC | 45 ms
54,904 KB |
testcase_09 | AC | 46 ms
54,528 KB |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | AC | 43 ms
54,372 KB |
testcase_13 | WA | - |
testcase_14 | AC | 44 ms
54,184 KB |
testcase_15 | AC | 49 ms
54,656 KB |
testcase_16 | AC | 47 ms
54,272 KB |
testcase_17 | AC | 98 ms
76,672 KB |
testcase_18 | AC | 85 ms
73,216 KB |
testcase_19 | AC | 49 ms
54,912 KB |
testcase_20 | AC | 100 ms
76,928 KB |
testcase_21 | AC | 110 ms
76,672 KB |
testcase_22 | AC | 110 ms
76,800 KB |
testcase_23 | AC | 215 ms
79,488 KB |
testcase_24 | AC | 205 ms
79,232 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) for i in range(N): for j in edge[i]: if uf.same(i,j): print('No') exit(0) 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]])