結果

問題 No.363 門松サイクル
ユーザー maspymaspy
提出日時 2020-05-16 16:12:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 845 ms / 4,000 ms
コード長 2,443 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 81,768 KB
実行使用メモリ 139,768 KB
最終ジャッジ日時 2023-10-23 17:56:39
合計ジャッジ時間 14,888 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,564 KB
testcase_01 AC 40 ms
53,564 KB
testcase_02 AC 96 ms
76,692 KB
testcase_03 AC 80 ms
73,388 KB
testcase_04 AC 99 ms
76,496 KB
testcase_05 AC 97 ms
76,488 KB
testcase_06 AC 108 ms
76,760 KB
testcase_07 AC 101 ms
76,488 KB
testcase_08 AC 114 ms
76,660 KB
testcase_09 AC 326 ms
95,876 KB
testcase_10 AC 433 ms
97,332 KB
testcase_11 AC 498 ms
127,364 KB
testcase_12 AC 622 ms
132,060 KB
testcase_13 AC 735 ms
132,592 KB
testcase_14 AC 756 ms
131,900 KB
testcase_15 AC 472 ms
124,064 KB
testcase_16 AC 346 ms
102,136 KB
testcase_17 AC 440 ms
139,768 KB
testcase_18 AC 809 ms
119,000 KB
testcase_19 AC 845 ms
119,584 KB
testcase_20 AC 817 ms
124,412 KB
testcase_21 AC 502 ms
117,808 KB
testcase_22 AC 503 ms
122,860 KB
testcase_23 AC 437 ms
130,840 KB
testcase_24 AC 40 ms
53,564 KB
testcase_25 AC 40 ms
53,564 KB
testcase_26 AC 362 ms
138,480 KB
testcase_27 AC 318 ms
138,480 KB
testcase_28 AC 617 ms
138,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
A = (0, ) + tuple(map(int, readline().split()))
G = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    x, y = map(int, readline().split())
    G[x].append(y)
    G[y].append(x)
Q = int(readline())
m = map(int, read().split())
query = tuple(zip(m, m))

parent = [0] * (N + 1)
depth = [0] * (N + 1)
stack = []
order = []
root = 1
stack = [root]
while stack:
    v = stack.pop()
    order.append(v)
    for w in G[v]:
        if w == parent[v]:
            continue
        parent[w] = v
        depth[w] = depth[v] + 1
        stack.append(w)

K = 18
P = [parent]
for _ in range(K):
    p = P[-1]
    P.append([p[v] for v in p])

def LCA(u, v):
    du, dv = depth[u], depth[v]
    if du < dv:
        du, dv = dv, du
        u, v = v, u
    n = du - dv
    for i in range(K):
        if n & 1:
            u = P[i][u]
        n >>= 1
    if u == v:
        return u
    for i in range(K - 1, -1, -1):
        u1, v1 = P[i][u], P[i][v]
        if u1 == v1:
            continue
        u, v = u1, v1
    return parent[u]

def is_kadomatsu(a, b, c):
    if a == c:
        return False
    return (a > b < c) or (a < b > c)

longest_kadomatsu = [0] * (N + 1)
for v in order[1:]:
    p = parent[v]
    pp = parent[p]
    if not p:
        longest_kadomatsu[v] = 0
    elif not pp:
        longest_kadomatsu[v] = 1
    else:
        a, b, c = A[v], A[p], A[pp]
        if is_kadomatsu(a, b, c):
            longest_kadomatsu[v] = longest_kadomatsu[p] + 1
        else:
            longest_kadomatsu[v] = 1

def ascend(v, k):
    for i in range(K):
        if k & 1:
            v = P[i][v]
        k >>= 1
    return v

longest_kadomatsu

def solve(u,v):
    w = LCA(u,v)
    if v == w:
        u,v = v,u
    du, dv, dw = depth[u], depth[v], depth[w]
    if longest_kadomatsu[v] < dv - dw:
        return False
    if longest_kadomatsu[u] < du - dw:
        return False
    if u == w:
        v1 = parent[v]
        v2 = ascend(v, dv-dw-1)
        return is_kadomatsu(A[v2], A[w], A[v]) and is_kadomatsu(A[w], A[v], A[v1])
    v1 = parent[v]
    v2 = ascend(v, dv-dw-1)
    u1 = parent[u]
    u2 = ascend(u, du-dw-1)
    return is_kadomatsu(A[v2], A[w], A[u2]) and is_kadomatsu(A[v], A[u], A[u1]) and is_kadomatsu(A[u], A[v], A[v1])        

query

for a,b in query:
    print('YES' if solve(a,b) else 'NO')
0