結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー tamatotamato
提出日時 2022-06-17 22:35:57
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 6,211 bytes
コンパイル時間 338 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 276,308 KB
最終ジャッジ日時 2024-10-09 08:51:45
合計ジャッジ時間 42,265 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
54,656 KB
testcase_01 AC 45 ms
54,784 KB
testcase_02 AC 46 ms
55,296 KB
testcase_03 AC 44 ms
54,912 KB
testcase_04 AC 46 ms
54,656 KB
testcase_05 AC 50 ms
55,040 KB
testcase_06 AC 46 ms
54,784 KB
testcase_07 AC 45 ms
54,784 KB
testcase_08 AC 251 ms
80,176 KB
testcase_09 AC 419 ms
86,156 KB
testcase_10 AC 296 ms
83,336 KB
testcase_11 AC 360 ms
84,048 KB
testcase_12 AC 401 ms
83,408 KB
testcase_13 AC 1,198 ms
151,772 KB
testcase_14 AC 1,252 ms
148,040 KB
testcase_15 AC 1,390 ms
192,212 KB
testcase_16 AC 1,196 ms
222,880 KB
testcase_17 AC 1,272 ms
146,332 KB
testcase_18 AC 1,306 ms
191,376 KB
testcase_19 AC 1,626 ms
209,376 KB
testcase_20 AC 1,306 ms
184,752 KB
testcase_21 AC 1,422 ms
172,380 KB
testcase_22 AC 1,443 ms
155,988 KB
testcase_23 AC 1,834 ms
204,772 KB
testcase_24 AC 1,829 ms
204,488 KB
testcase_25 AC 1,799 ms
201,704 KB
testcase_26 AC 1,824 ms
202,964 KB
testcase_27 AC 1,833 ms
202,932 KB
testcase_28 AC 1,828 ms
204,572 KB
testcase_29 WA -
testcase_30 AC 1,824 ms
203,440 KB
testcase_31 AC 1,819 ms
202,844 KB
testcase_32 AC 1,943 ms
203,656 KB
testcase_33 AC 44 ms
54,656 KB
testcase_34 AC 791 ms
236,008 KB
testcase_35 AC 556 ms
173,112 KB
testcase_36 AC 779 ms
167,136 KB
testcase_37 AC 44 ms
55,040 KB
testcase_38 AC 219 ms
77,312 KB
testcase_39 AC 1,097 ms
276,308 KB
testcase_40 AC 864 ms
270,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
    OK_XY = set()
    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)
        E = set()
        for v in range(1, NNN + 1):
            for u in adj_ori[v]:
                E.add(v * (N + 1) + u)
        for v in range(1, NN + 1):
            for u in adj[v]:
                vv = seq[v - 1]
                uu = seq[u - 1]
                i = idx_new[v]
                j = idx_new[u]
                if i * (N + 1) + j in E:
                    OK_XY.add(vv * (N + 1) + uu)
                    OK_XY.add(uu * (N + 1) + vv)

    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:
            xy = x * (N + 1) + y
            if xy in OK_XY:
                print("Yes")
            else:
                print("No")


if __name__ == '__main__':
    main()
0