Submission #64790120


Source Code Expand

Copy
import sys
sys.setrecursionlimit(10**6)
import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')
def all_direction_tree_dp(n, edges):
G = [[] for _ in range(n)]
for u, v in edges:
G[u].append(v)
G[v].append(u)
dp = [0] * n
ans = [0] * n
def dfs1(v, parent):
for w in G[v]:
if w == parent:
continue
dfs1(w, v)
dp[v] = max(dp[v], dp[w] + 1)
def dfs2(v, parent, up):
ans[v] = max(dp[v], up)
children = [w for w in G[v] if w != parent]
m = len(children)
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
import sys
sys.setrecursionlimit(10**6)
import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')
def all_direction_tree_dp(n, edges):
    G = [[] for _ in range(n)]
    for u, v in edges:
        G[u].append(v)
        G[v].append(u)
    dp = [0] * n
    ans = [0] * n
    def dfs1(v, parent):
        for w in G[v]:
            if w == parent:
                continue
            dfs1(w, v)
            dp[v] = max(dp[v], dp[w] + 1)   
    def dfs2(v, parent, up):
        ans[v] = max(dp[v], up)
        children = [w for w in G[v] if w != parent]
        m = len(children)
        cand = [dp[w] + 2 for w in children]       
        prefix = [0] * (m + 1)
        suffix = [0] * (m + 1)
        for i in range(m):
            prefix[i+1] = max(prefix[i], cand[i])
        for i in range(m-1, -1, -1):
            suffix[i] = max(suffix[i+1], cand[i])
        
        for i, w in enumerate(children):
            siblings_max = max(prefix[i], suffix[i+1])
            new_up = max(up + 1, siblings_max)
            dfs2(w, v, new_up)
    
    dfs1(0, -1)
    dfs2(0, -1, 0)
    
    return ans

ans=0
n1=int(input())
g1=[]
for _ in range(n1-1):
    u,v=map(int,input().split())
    u-=1
    v-=1
    g1.append((u,v))
n2=int(input())
g2=[]
for _ in range(n2-1):
    u,v=map(int,input().split())
    u-=1
    v-=1
    g2.append((u,v))
ans=n1*n2
for c in all_direction_tree_dp(n1,g1):
    ans+=n2*c
for c in all_direction_tree_dp(n2,g2):
    ans+=n1*c
print(ans)

Submission Info

Submission Time
Task F - Add One Edge 3
User juntenbanana
Language Python (PyPy 3.10-v7.3.12)
Score 0
Code Size 1525 Byte
Status WA
Exec Time 2246 ms
Memory 605488 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 2
AC × 21
WA × 20
TLE × 8
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 57 ms 76372 KB
00_sample_01.txt AC 57 ms 76616 KB
01_random_00.txt AC 919 ms 170868 KB
01_random_01.txt AC 300 ms 101804 KB
01_random_02.txt AC 225 ms 93624 KB
01_random_03.txt WA 131 ms 85272 KB
01_random_04.txt AC 866 ms 163480 KB
01_random_05.txt WA 586 ms 134384 KB
01_random_06.txt WA 456 ms 120856 KB
01_random_07.txt WA 397 ms 114860 KB
01_random_08.txt AC 475 ms 122460 KB
01_random_09.txt WA 397 ms 116152 KB
01_random_10.txt AC 838 ms 192408 KB
01_random_11.txt AC 404 ms 131180 KB
01_random_12.txt AC 664 ms 167120 KB
01_random_13.txt AC 224 ms 101200 KB
01_random_14.txt AC 614 ms 163456 KB
01_random_15.txt AC 282 ms 111284 KB
01_random_16.txt AC 487 ms 148432 KB
01_random_17.txt AC 341 ms 124136 KB
01_random_18.txt AC 807 ms 189832 KB
01_random_19.txt AC 582 ms 167904 KB
01_random_20.txt AC 921 ms 169596 KB
01_random_21.txt WA 635 ms 139024 KB
01_random_22.txt WA 314 ms 104412 KB
01_random_23.txt WA 332 ms 106228 KB
01_random_24.txt AC 518 ms 126028 KB
01_random_25.txt WA 555 ms 130000 KB
01_random_26.txt WA 592 ms 135680 KB
01_random_27.txt WA 522 ms 128588 KB
01_random_28.txt WA 491 ms 124496 KB
01_random_29.txt AC 333 ms 104048 KB
01_random_30.txt TLE 2246 ms 600472 KB
01_random_31.txt TLE 2244 ms 579856 KB
01_random_32.txt WA 1864 ms 431376 KB
01_random_33.txt TLE 2237 ms 472788 KB
01_random_34.txt WA 1432 ms 421116 KB
01_random_35.txt WA 642 ms 230992 KB
01_random_36.txt WA 1189 ms 402276 KB
01_random_37.txt WA 899 ms 310680 KB
01_random_38.txt WA 931 ms 343672 KB
01_random_39.txt WA 1033 ms 349916 KB
01_random_40.txt TLE 2020 ms 549076 KB
01_random_41.txt TLE 2246 ms 605488 KB
01_random_42.txt TLE 2236 ms 456592 KB
01_random_43.txt TLE 2235 ms 443492 KB
01_random_44.txt TLE 2237 ms 474252 KB
01_random_45.txt AC 57 ms 76684 KB
01_random_46.txt WA 1603 ms 530664 KB


2025-04-26 (Sat)
20:45:06 +09:00