結果
問題 | No.1983 [Cherry 4th Tune C] 南の島のマーメイド |
ユーザー | tamato |
提出日時 | 2022-06-17 22:25:19 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,679 bytes |
コンパイル時間 | 420 ms |
コンパイル使用メモリ | 82,436 KB |
実行使用メモリ | 260,012 KB |
最終ジャッジ日時 | 2024-10-09 08:35:51 |
合計ジャッジ時間 | 36,715 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
54,784 KB |
testcase_01 | AC | 43 ms
54,656 KB |
testcase_02 | AC | 41 ms
54,656 KB |
testcase_03 | AC | 41 ms
54,400 KB |
testcase_04 | AC | 41 ms
54,784 KB |
testcase_05 | AC | 42 ms
55,040 KB |
testcase_06 | AC | 42 ms
54,784 KB |
testcase_07 | AC | 42 ms
54,912 KB |
testcase_08 | AC | 226 ms
80,580 KB |
testcase_09 | AC | 337 ms
86,040 KB |
testcase_10 | AC | 266 ms
83,780 KB |
testcase_11 | AC | 318 ms
84,296 KB |
testcase_12 | AC | 336 ms
83,384 KB |
testcase_13 | AC | 1,007 ms
148,036 KB |
testcase_14 | AC | 1,033 ms
144,940 KB |
testcase_15 | AC | 1,209 ms
191,488 KB |
testcase_16 | AC | 1,050 ms
221,192 KB |
testcase_17 | AC | 1,104 ms
142,968 KB |
testcase_18 | AC | 1,175 ms
190,212 KB |
testcase_19 | AC | 1,378 ms
206,008 KB |
testcase_20 | AC | 1,170 ms
183,500 KB |
testcase_21 | AC | 1,192 ms
167,688 KB |
testcase_22 | AC | 1,239 ms
153,344 KB |
testcase_23 | AC | 1,644 ms
198,996 KB |
testcase_24 | AC | 1,543 ms
198,324 KB |
testcase_25 | AC | 1,578 ms
197,696 KB |
testcase_26 | AC | 1,559 ms
197,068 KB |
testcase_27 | AC | 1,593 ms
199,464 KB |
testcase_28 | AC | 1,559 ms
198,660 KB |
testcase_29 | WA | - |
testcase_30 | AC | 1,582 ms
199,096 KB |
testcase_31 | AC | 1,558 ms
200,464 KB |
testcase_32 | AC | 1,636 ms
199,644 KB |
testcase_33 | AC | 42 ms
54,400 KB |
testcase_34 | AC | 680 ms
235,580 KB |
testcase_35 | AC | 510 ms
172,968 KB |
testcase_36 | AC | 700 ms
166,060 KB |
testcase_37 | AC | 43 ms
54,784 KB |
testcase_38 | AC | 204 ms
77,312 KB |
testcase_39 | AC | 809 ms
260,012 KB |
testcase_40 | AC | 766 ms
259,276 KB |
ソースコード
mod = 998244353 def main(): import sys from collections import deque input = sys.stdin.buffer.readline # return: articulation points, bridges # The graph must be connected. def lowlink(adj, root=1): N = len(adj) - 1 order = [N + 1] * (N + 1) low = [N + 1] * (N + 1) AP = [] bridge = [] st = [root] cnt = 1 par = [0] * (N + 1) seq = [] while st: v = st.pop() if order[v] != N + 1: continue order[v] = cnt seq.append(v) low[v] = cnt cnt += 1 for u in adj[v]: if order[u] < cnt: if par[v] != u: low[v] = min(low[v], order[u]) continue else: par[u] = v st.append(u) child = [[] for _ in range(N + 1)] for v in range(1, N + 1): child[par[v]].append(v) seq.reverse() for v in seq: for u in child[v]: low[v] = min(low[v], low[u]) # bridge for p in range(1, N + 1): for c in child[p]: if order[p] < low[c]: bridge.append((p, c)) # articulation point for v in range(1, N + 1): if v == root: if len(child[v]) > 1: AP.append(v) else: for c in child[v]: if order[v] <= low[c]: AP.append(v) break return AP, bridge # idx_new[v]: adj_new(二辺連結成分分解後のグラフ)で元のグラフのvがどの頂点に入るか def two_edge_connected_components(adj, bridge): N = len(adj) - 1 cnt = 0 idx_new = [-1] * (N + 1) seen = [0] * (N + 1) B = set() for u, v in bridge: B.add(u * (N + 1) + v) B.add(v * (N + 1) + u) for v0 in range(1, N + 1): if seen[v0]: continue seen[v0] = 1 st = [v0] cnt += 1 while st: v = st.pop() idx_new[v] = cnt for u in adj[v]: if not seen[u]: if v * (N + 1) + u in B: continue seen[u] = 1 st.append(u) adj_new = [[] for _ in range(cnt + 1)] for u, v in bridge: u_new = idx_new[u] v_new = idx_new[v] adj_new[u_new].append(v_new) adj_new[v_new].append(u_new) return adj_new, idx_new class UnionFind(): def __init__(self, n): self.n = n self.root = [-1] * (n + 1) self.rnk = [0] * (n + 1) def find_root(self, x): while self.root[x] >= 0: x = self.root[x] return x def unite(self, x, y): x = self.find_root(x) y = self.find_root(y) if x == y: return elif self.rnk[x] > self.rnk[y]: self.root[x] += self.root[y] self.root[y] = x else: self.root[y] += self.root[x] self.root[x] = y if self.rnk[x] == self.rnk[y]: self.rnk[y] += 1 def isSameGroup(self, x, y): return self.find_root(x) == self.find_root(y) def size(self, x): return -self.root[self.find_root(x)] N, M, Q = map(int, input().split()) adj_ori = [[] for _ in range(N + 1)] UF_ori = UnionFind(N) for _ in range(M): a, b = map(int, input().split()) adj_ori[a].append(b) adj_ori[b].append(a) UF_ori.unite(a, b) adj_dict = {} idx_dict = {} UF = UnionFind(N) seen = [0] * (N + 1) V0 = [-1] * (N + 1) for v0 in range(1, N + 1): if seen[v0]: continue que = deque() que.append(v0) seen[v0] = 1 seq = [] while que: v = que.popleft() V0[v] = v0 seq.append(v) for u in adj_ori[v]: if not seen[u]: que.append(u) seen[u] = 1 idx = {v: i+1 for i, v in enumerate(seq)} idx_dict[v0] = idx NN = len(seq) adj = [[] for _ in range(NN + 1)] for v in seq: i = idx[v] for u in adj_ori[v]: j = idx[u] adj[i].append(j) adj_dict[v0] = adj AP, bridge = lowlink(adj, root=1) adj_new, idx_new = two_edge_connected_components(adj, bridge) NNN = len(adj_new) - 1 cnt = [0] * (NNN + 1) v_convert = {} ii = 0 for v in idx_new[1:]: ii += 1 cnt[v] += 1 v_convert[v] = ii V_OK = [] for v in range(1, NNN + 1): if cnt[v] == 1: V_OK.append(v) for v in V_OK: v_ori = v_convert[v] for u in adj[v_ori]: vv = seq[v_ori - 1] uu = seq[u - 1] UF.unite(vv, uu) #print(vv, uu) #print("---") for _ in range(Q): x, y = map(int, input().split()) if not UF_ori.isSameGroup(x, y): print("No") continue if UF.isSameGroup(x, y): print("Yes") else: print("No") if __name__ == '__main__': main()