結果
問題 | No.1983 [Cherry 4th Tune C] 南の島のマーメイド |
ユーザー | lilictaka |
提出日時 | 2022-06-20 12:42:48 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,629 bytes |
コンパイル時間 | 142 ms |
コンパイル使用メモリ | 82,244 KB |
実行使用メモリ | 313,084 KB |
最終ジャッジ日時 | 2024-10-13 00:26:40 |
合計ジャッジ時間 | 41,979 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
55,428 KB |
testcase_01 | AC | 41 ms
55,756 KB |
testcase_02 | AC | 38 ms
55,308 KB |
testcase_03 | AC | 39 ms
56,288 KB |
testcase_04 | AC | 39 ms
56,252 KB |
testcase_05 | AC | 39 ms
55,740 KB |
testcase_06 | WA | - |
testcase_07 | AC | 40 ms
55,140 KB |
testcase_08 | WA | - |
testcase_09 | AC | 222 ms
84,720 KB |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | AC | 635 ms
203,288 KB |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | AC | 39 ms
55,768 KB |
testcase_34 | AC | 449 ms
217,228 KB |
testcase_35 | AC | 744 ms
267,800 KB |
testcase_36 | AC | 887 ms
282,196 KB |
testcase_37 | AC | 38 ms
56,128 KB |
testcase_38 | WA | - |
testcase_39 | AC | 797 ms
281,132 KB |
testcase_40 | AC | 805 ms
313,084 KB |
ソースコード
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()) def scc_decomposition(G): n = len(G) G_rev = [[] for _ in range(n)] for u in range(n): for v in G[u]: G_rev[v].append(u) # dfs vs = [] visited = [False] * n used = [False] * n for u in range(n): if visited[u]: continue stack = [u] while stack: v = stack.pop() if used[v]: continue if not visited[v]: visited[v] = True else: vs.append(v) used[v] = True continue stack.append(v) for c in G[v]: if not visited[c]: stack.append(c) # reverse dfs visited = [False] * n component = [-1] * n k = 0 for u in vs[::-1]: if visited[u]: continue stack = [u] while stack: v = stack.pop() visited[v] = True component[v] = k for c in G_rev[v]: if not visited[c]: stack.append(c) k += 1 return component # 強連結成分分解(SCC): グラフGに対するSCCを行う # 入力: <N>: 頂点サイズ, <G>: 順方向の有向グラフ, <RG>: 逆方向の有向グラフ # 出力: (<ラベル数>, <各頂点のラベル番号>) def scc(N, G, RG): order = [] used = [0]*N group = [None]*N def dfs(s): used[s] = 1 for t in G[s]: if not used[t]: dfs(t) order.append(s) def rdfs(s, col): group[s] = col used[s] = 1 for t in RG[s]: if not used[t]: rdfs(t, col) for i in range(N): if not used[i]: dfs(i) used = [0]*N label = 0 for s in reversed(order): if not used[s]: rdfs(s, label) label += 1 return label, group N,M,Q = map(int,input().split()) G0 = [[] for _ in range(N)] for _ in range(M): u,v = map(lambda x:int(x)-1,input().split()) G0[u].append(v) G0[v].append(u) query = [] for _ in range(Q): x,y = map(lambda x:int(x)-1,input().split()) query.append((x,y)) cmp = scc_decomposition(G0) cmpmemo = defaultdict(list) for i in range(N): cmpmemo[cmp[i]].append(i) G = [[] for _ in range(N)] RG = [[] for _ in range(N)] seen = [0] * N for key in cmpmemo.keys(): st = cmpmemo[key][0] q = deque() q.append((st,-1)) while q: now,back = q.popleft() if back != -1: G[back].append(now) RG[now].append(back) if seen[now]: continue seen[now] += 1 for nx in G0[now]: if nx == back: continue if seen[nx] <= 1: q.appendleft((nx,now)) cmp = scc_decomposition(G) cnt = defaultdict(int) for i in range(N): cnt[cmp[i]] += 1 uf = UnionFind(N) G0set = [set() for _ in range(N)] for u in range(N): for v in G0[u]: G0set[u].add(v) if cmp[u] == cmp[v]: continue if cnt[cmp[u]] >= 2 or cnt[cmp[v]] >= 2: continue uf.union(u,v) for x,y in query: if uf.same(x,y): print('Yes') else: if y in G0set[x] and cmp[x] != cmp[y]: print('Yes') else: print('No')