結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー tamatotamato
提出日時 2022-06-17 22:51:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,282 ms / 4,000 ms
コード長 6,438 bytes
コンパイル時間 384 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 233,916 KB
最終ジャッジ日時 2024-10-09 09:12:21
合計ジャッジ時間 30,927 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
54,784 KB
testcase_01 AC 48 ms
54,272 KB
testcase_02 AC 46 ms
54,656 KB
testcase_03 AC 48 ms
54,784 KB
testcase_04 AC 47 ms
54,784 KB
testcase_05 AC 47 ms
54,400 KB
testcase_06 AC 46 ms
54,656 KB
testcase_07 AC 48 ms
54,656 KB
testcase_08 AC 213 ms
79,440 KB
testcase_09 AC 321 ms
84,720 KB
testcase_10 AC 245 ms
82,312 KB
testcase_11 AC 288 ms
82,980 KB
testcase_12 AC 293 ms
81,868 KB
testcase_13 AC 930 ms
137,500 KB
testcase_14 AC 908 ms
134,908 KB
testcase_15 AC 1,109 ms
184,248 KB
testcase_16 AC 941 ms
219,868 KB
testcase_17 AC 850 ms
133,880 KB
testcase_18 AC 956 ms
189,232 KB
testcase_19 AC 1,164 ms
184,948 KB
testcase_20 AC 935 ms
175,936 KB
testcase_21 AC 929 ms
154,688 KB
testcase_22 AC 1,098 ms
144,980 KB
testcase_23 AC 1,227 ms
180,688 KB
testcase_24 AC 1,250 ms
180,776 KB
testcase_25 AC 1,230 ms
182,256 KB
testcase_26 AC 1,241 ms
180,636 KB
testcase_27 AC 1,237 ms
180,612 KB
testcase_28 AC 1,263 ms
180,652 KB
testcase_29 AC 1,267 ms
180,584 KB
testcase_30 AC 1,278 ms
180,192 KB
testcase_31 AC 1,208 ms
178,572 KB
testcase_32 AC 1,282 ms
181,292 KB
testcase_33 AC 46 ms
54,912 KB
testcase_34 AC 580 ms
233,916 KB
testcase_35 AC 495 ms
170,388 KB
testcase_36 AC 607 ms
166,340 KB
testcase_37 AC 45 ms
54,400 KB
testcase_38 AC 230 ms
77,576 KB
testcase_39 AC 564 ms
187,032 KB
testcase_40 AC 551 ms
189,636 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)
        for u, v in bridge:
            uu, vv = seq[u - 1], seq[v - 1]
            UF.unite(uu, vv)
        """
        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)
                    UF.unite(uu, 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:
            print("No")
            #xy = x * (N + 1) + y
            #if xy in OK_XY:
            #    print("Yes")
            #else:
            #    print("No")
    print(UF.root, file=sys.stderr)


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