結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー 👑 tamatotamato
提出日時 2022-06-17 22:51:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,534 ms / 4,000 ms
コード長 6,438 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 82,476 KB
実行使用メモリ 234,080 KB
最終ジャッジ日時 2024-04-17 15:12:23
合計ジャッジ時間 35,418 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
54,912 KB
testcase_01 AC 48 ms
55,168 KB
testcase_02 AC 48 ms
54,656 KB
testcase_03 AC 49 ms
55,040 KB
testcase_04 AC 49 ms
54,656 KB
testcase_05 AC 53 ms
54,656 KB
testcase_06 AC 48 ms
55,168 KB
testcase_07 AC 49 ms
54,912 KB
testcase_08 AC 218 ms
79,696 KB
testcase_09 AC 323 ms
84,848 KB
testcase_10 AC 257 ms
82,692 KB
testcase_11 AC 298 ms
83,240 KB
testcase_12 AC 301 ms
81,996 KB
testcase_13 AC 964 ms
137,756 KB
testcase_14 AC 1,067 ms
135,300 KB
testcase_15 AC 1,116 ms
184,628 KB
testcase_16 AC 929 ms
220,120 KB
testcase_17 AC 1,001 ms
134,136 KB
testcase_18 AC 1,065 ms
189,356 KB
testcase_19 AC 1,302 ms
185,208 KB
testcase_20 AC 1,058 ms
175,936 KB
testcase_21 AC 1,115 ms
154,944 KB
testcase_22 AC 1,289 ms
145,120 KB
testcase_23 AC 1,518 ms
180,680 KB
testcase_24 AC 1,506 ms
181,280 KB
testcase_25 AC 1,480 ms
183,012 KB
testcase_26 AC 1,516 ms
181,264 KB
testcase_27 AC 1,534 ms
181,112 KB
testcase_28 AC 1,524 ms
181,160 KB
testcase_29 AC 1,518 ms
180,972 KB
testcase_30 AC 1,520 ms
180,580 KB
testcase_31 AC 1,436 ms
178,568 KB
testcase_32 AC 1,463 ms
181,000 KB
testcase_33 AC 48 ms
54,912 KB
testcase_34 AC 695 ms
234,080 KB
testcase_35 AC 538 ms
170,900 KB
testcase_36 AC 682 ms
166,796 KB
testcase_37 AC 49 ms
55,168 KB
testcase_38 AC 247 ms
78,100 KB
testcase_39 AC 625 ms
187,032 KB
testcase_40 AC 664 ms
189,528 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