結果

問題 No.1494 LCS on Tree
ユーザー shotoyooshotoyoo
提出日時 2021-04-30 22:19:48
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,206 bytes
コンパイル時間 450 ms
コンパイル使用メモリ 86,364 KB
実行使用メモリ 210,388 KB
最終ジャッジ日時 2023-09-26 06:26:03
合計ジャッジ時間 40,250 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,424 KB
testcase_01 AC 76 ms
70,712 KB
testcase_02 AC 75 ms
71,028 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 74 ms
71,200 KB
testcase_21 AC 73 ms
71,148 KB
testcase_22 WA -
testcase_23 AC 74 ms
71,176 KB
testcase_24 AC 74 ms
70,860 KB
testcase_25 WA -
testcase_26 AC 75 ms
70,996 KB
testcase_27 WA -
testcase_28 AC 73 ms
71,044 KB
testcase_29 AC 73 ms
71,208 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 TLE -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")

# 木の読み込み・重みあり
n = int(input())
s = [(ord(c)-ord("a")) for c in input()]
m = len(s)
ns = [[] for _ in range(n)]
for _ in range(n-1):
    u,v,c = input().split()
    c = ord(c) - ord("a")
    u = int(u)
    v = int(v)
    u -= 1
    v -= 1
    ns[u].append((c,v))
    ns[v].append((c,u))
dp0 = [-1]*n
dp1 = [-1]*n
ans = 0
def sub(l,c):
    nl = [0]
    for i in range(m):
        if s[i]==c:
            if i>=1:
                v = l[i-1]+1
            else:
                v = 1
        else:
            v = l[i]
        nl.append(max(v, nl[-1]))
    return nl
def sub2(l,c):
    nl = [0]
    for i in range(m)[::-1]:
        if s[i]==c:
            if i+1<m:
                v = l[i+1]+1
            else:
                v = 1
        else:
            v = l[i]
        nl.append(max(v, nl[-1]))
    return nl[::-1]
def sub3(l0,l1):
    k = len(l0)
    ans = 0
    for i in range(k):
        for j in range(k):
            if i==j:
                continue
            ans = max(ans, max((l0[i][ind]+l1[j][ind] for ind in range(len(l0)))))
    ans = max(ans, max((item[-1] for item in l0)), max((item[0] for item in l1)))
    return ans
# 深さ優先探索 (巻き戻しあり) dfs tree
q = [(0, -1)]
while q:
    u,prv = q.pop()
    if u<0:
        # 返るときの処理
        u = ~u
        l0 = []
        l1 = []
        for c,v in ns[u]:
            if v==prv:
                continue
            v0 = sub(dp0[v],c)
            v1 = sub2(dp1[v],c)
            l0.append(v0)
            l1.append(v1)
#             print(s,c,v0,v1)
        if l0:
            ans = max(ans, sub3(l0,l1))
            dp0[u] = [max(item[i] for item in l0) for i in range(m+1)]
            dp1[u] = [max(item[i] for item in l1) for i in range(m+1)]
        else:
            dp0[u] = [0]*(m+1)
            dp1[u] = [0]*(m+1)
    else:
        q.append((~u,prv))
        for c,v in ns[u]:
            # 進むときの処理
            if v==prv:
                continue
            q.append((v,u))
print(ans)
0