結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー mkawa2mkawa2
提出日時 2023-11-11 18:45:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 784 ms / 4,000 ms
コード長 3,405 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 82,308 KB
実行使用メモリ 152,916 KB
最終ジャッジ日時 2024-09-26 02:50:43
合計ジャッジ時間 20,851 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,992 KB
testcase_01 AC 36 ms
52,992 KB
testcase_02 AC 35 ms
52,480 KB
testcase_03 AC 36 ms
53,248 KB
testcase_04 AC 36 ms
53,504 KB
testcase_05 AC 35 ms
52,864 KB
testcase_06 AC 34 ms
52,736 KB
testcase_07 AC 35 ms
52,992 KB
testcase_08 AC 141 ms
77,824 KB
testcase_09 AC 140 ms
79,024 KB
testcase_10 AC 202 ms
81,208 KB
testcase_11 AC 201 ms
80,796 KB
testcase_12 AC 167 ms
79,360 KB
testcase_13 AC 485 ms
106,224 KB
testcase_14 AC 543 ms
107,416 KB
testcase_15 AC 546 ms
121,024 KB
testcase_16 AC 276 ms
105,548 KB
testcase_17 AC 552 ms
109,268 KB
testcase_18 AC 478 ms
120,460 KB
testcase_19 AC 626 ms
127,596 KB
testcase_20 AC 483 ms
118,424 KB
testcase_21 AC 570 ms
115,164 KB
testcase_22 AC 633 ms
119,712 KB
testcase_23 AC 774 ms
129,524 KB
testcase_24 AC 778 ms
131,000 KB
testcase_25 AC 744 ms
128,736 KB
testcase_26 AC 743 ms
129,116 KB
testcase_27 AC 741 ms
129,252 KB
testcase_28 AC 784 ms
129,080 KB
testcase_29 AC 781 ms
129,376 KB
testcase_30 AC 750 ms
129,196 KB
testcase_31 AC 756 ms
129,268 KB
testcase_32 AC 739 ms
129,124 KB
testcase_33 AC 36 ms
54,792 KB
testcase_34 AC 190 ms
102,244 KB
testcase_35 AC 341 ms
151,740 KB
testcase_36 AC 351 ms
120,280 KB
testcase_37 AC 38 ms
53,144 KB
testcase_38 AC 181 ms
77,836 KB
testcase_39 AC 413 ms
151,468 KB
testcase_40 AC 402 ms
152,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(1000005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

class UnionFind:
    def __init__(self, n):
        self._tree = [-1]*n
        # number of connected component
        self.cnt = n

    def root(self, u):
        stack = []
        while self._tree[u] >= 0:
            stack.append(u)
            u = self._tree[u]
        for v in stack: self._tree[v] = u
        return u

    def same(self, u, v):
        return self.root(u) == self.root(v)

    def merge(self, u, v):
        u, v = self.root(u), self.root(v)
        if u == v: return False
        if self._tree[u] > self._tree[v]: u, v = v, u
        self._tree[u] += self._tree[v]
        self._tree[v] = u
        self.cnt -= 1
        return True

    # size of connected component
    def size(self, u):
        return -self._tree[self.root(u)]

class LCA:
    def __init__(self, parents, depth):
        n = len(parents)
        self._depth = depth
        log = max(self._depth).bit_length()
        self._table = [parents]+[[-1]*n for _ in range(log)]
        row0 = self._table[0]
        for lv in range(log):
            row1 = self._table[lv+1]
            for u in range(n):
                if row0[u] == -1: continue
                row1[u] = row0[row0[u]]
            row0 = row1

    def anc(self, u, v):
        diff = self._depth[u]-self._depth[v]
        if diff < 0: u, v = v, u
        diff = abs(diff)
        u = self.up(u, diff)
        if u == v: return u
        for lv in range(self._depth[u].bit_length()-1, -1, -1):
            anclv = self._table[lv]
            if anclv[u] != anclv[v]: u, v = anclv[u], anclv[v]
        return self._table[0][u]

    def up(self, u, dist):
        lv = 0
        while dist and u != -1:
            if dist & 1: u = self._table[lv][u]
            lv, dist = lv+1, dist >> 1
        return u

n,m,q=LI()
to=[[] for _ in range(n)]
uv=[]
uf=UnionFind(n)
for _ in range(m):
    u,v=LI1()
    if uf.merge(u,v):
        to[u].append(v)
        to[v].append(u)
    else:
        uv.append((u,v))

def dfs(root=0):
    uu, stack = [], [root]
    while stack:
        u = stack.pop()
        uu.append(u)
        for v in to[u]:
            if v == parent[u]: continue
            parent[v] = u
            depth[v] = depth[u]+1
            stack.append(v)
    return uu

parent, depth = [-1]*n, [0]*n
uu=[]
for u in range(n):
    if parent[u]==-1:uu+=dfs(u)

lca=LCA(parent,depth)
dp=[0]*n
for u,v in uv:
    a=lca.anc(u,v)
    dp[u]+=1
    dp[v]+=1
    dp[a]-=2

uf=UnionFind(n)
for u in uu[::-1]:
    p=parent[u]
    if p==-1:continue
    dp[p]+=dp[u]
    if dp[u]:continue
    uf.merge(p,u)

for _ in range(q):
    u,v=LI1()
    print("Yes" if uf.same(u,v) else "No")
0