Submission #73714907


Source Code Expand

from math import inf
import sys
import random

r32 = random.getrandbits(32)
r64 = random.getrandbits(64)
def input(): return sys.stdin.readline().rstrip("\r\n")
def rint(): return int(input())
def rf(): return float(input())
def rs(): return input().split()
def rmap(): return map(int, input().split())
def rmapf(): return map(float, input().split())
def rlist(): return list(map(int, input().split()))
def rlistf(): return list(map(float, input().split()))
def rmap1(): return map(lambda x: int(x)-1, input().split())
def rlist1(): return list(map(lambda x: int(x)-1, input().split()))
def fmin(a, b): return a if a < b else b
def fmax(a, b): return a if a > b else b

def exclusive(A, zero, combine, node):
    n = len(A)
    exclusiveA = [zero] * n  # Exclusive segment tree

    # Build exclusive segment tree
    for bit in range(n.bit_length())[::-1]:
        for i in range(n)[::-1]:
            # Propagate values down the segment tree
            exclusiveA[i] = exclusiveA[i // 2]
        for i in range(n & ~(bit == 0)):
            # Fold A[i] into exclusive segment tree
            ind = (i >> bit) ^ 1
            exclusiveA[ind] = combine(exclusiveA[ind], A[i], node, i)
    return exclusiveA


def rerooter(graph, default, combine, finalize=lambda nodeDP, node, eind: nodeDP):
    n = len(graph)
    rootDP = [0] * n
    forwardDP = [None] * n
    reverseDP = [None] * n

    # Compute DP for root=0
    DP = [0] * n
    bfs = [0]
    P = [0] * n
    for node in bfs:
        for nei in graph[node]:
            if P[node] != nei:
                P[nei] = node
                bfs.append(nei)

    for node in reversed(bfs):
        nodeDP = default[node]
        for eind, nei in enumerate(graph[node]):
            if P[node] != nei:
                nodeDP = combine(nodeDP, DP[nei], node, eind)
        DP[node] = finalize(nodeDP, node, graph[node].index(P[node]) if node else -1)
    # DP for root=0 done

    # Use the exclusive function to reroot
    for node in bfs:
        DP[P[node]] = DP[node]
        forwardDP[node] = [DP[nei] for nei in graph[node]]
        rerootDP = exclusive(forwardDP[node], default[node], combine, node)
        reverseDP[node] = [
            finalize(nodeDP, node, eind) for eind, nodeDP in enumerate(rerootDP)
        ]
        rootDP[node] = finalize(
            (
                combine(rerootDP[0], forwardDP[node][0], node, 0)
                if n > 1
                else default[node]
            ),
            node,
            -1,
        )
        for nei, dp in zip(graph[node], reverseDP[node]):
            DP[nei] = dp
    return rootDP, forwardDP, reverseDP




def solve():
    n = rint()
    g=[[] for _ in range(n)]
    for _ in range(n-1):
        u,v=rlist1()
        g[u].append(v)
        g[v].append(u)
        
    default = [[0,0]] * n
    
    def combine(nodeDP, childDP, node, eind):
        ret=nodeDP.copy()
        if len(g[node])>=4:
            ret[1]=max(ret[1],childDP[1]+1) 
            ret[0]=max(ret[0],childDP[1]+1)
        if len(g[node])>=3:
            ret[0]=max(ret[0],childDP[1]+1)
            ret[1]=max(ret[1],1)
        ret[0]=max(ret[0],childDP[0])
        return ret

    rootDP, forwardDP, reverseDP = rerooter(g, default, combine)
    ans=0
    for best,x in rootDP:
        ans=max(ans,best)
    print(ans)

t = 1

t = rint()

for _ in range(t):
    solve()

Submission Info

Submission Time
Task F - Centipede Graph
User Sandeep_P
Language Python (PyPy 3.11-v7.3.20)
Score 0
Code Size 3483 Byte
Status WA
Exec Time 1063 ms
Memory 230592 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 1
AC × 37
WA × 10
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 03_path_00.txt, 03_path_01.txt, 03_path_02.txt, 03_path_03.txt, 03_path_04.txt, 03_path_05.txt, 03_path_06.txt, 03_path_07.txt, 03_path_08.txt, 03_path_09.txt, 04_star_00.txt, 04_star_01.txt, 04_star_02.txt, 05_uni_00.txt, 05_uni_01.txt, 05_uni_02.txt, 05_uni_03.txt, 05_uni_04.txt, 05_uni_05.txt, 06_bi-ternary_00.txt, 06_bi-ternary_01.txt, 06_bi-ternary_02.txt, 06_bi-ternary_03.txt, 06_bi-ternary_04.txt, 06_bi-ternary_05.txt, 06_bi-ternary_06.txt, 06_bi-ternary_07.txt, 06_bi-ternary_08.txt, 06_bi-ternary_09.txt, 06_bi-ternary_10.txt, 06_bi-ternary_11.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 74 ms 97932 KiB
01_small_00.txt AC 449 ms 114780 KiB
01_small_01.txt AC 427 ms 114040 KiB
01_small_02.txt WA 445 ms 114504 KiB
01_small_03.txt AC 431 ms 113884 KiB
01_small_04.txt WA 438 ms 114472 KiB
01_small_05.txt AC 465 ms 114608 KiB
01_small_06.txt WA 373 ms 113592 KiB
02_random_00.txt WA 466 ms 121932 KiB
02_random_01.txt WA 452 ms 120504 KiB
02_random_02.txt WA 447 ms 120356 KiB
02_random_03.txt WA 1063 ms 225480 KiB
02_random_04.txt WA 1005 ms 228084 KiB
02_random_05.txt WA 1003 ms 229840 KiB
02_random_06.txt AC 1014 ms 230592 KiB
02_random_07.txt WA 976 ms 228672 KiB
03_path_00.txt AC 421 ms 119472 KiB
03_path_01.txt AC 438 ms 119528 KiB
03_path_02.txt AC 631 ms 184540 KiB
03_path_03.txt AC 658 ms 190256 KiB
03_path_04.txt AC 825 ms 215128 KiB
03_path_05.txt AC 831 ms 221952 KiB
03_path_06.txt AC 804 ms 221636 KiB
03_path_07.txt AC 863 ms 219044 KiB
03_path_08.txt AC 802 ms 221512 KiB
03_path_09.txt AC 863 ms 218608 KiB
04_star_00.txt AC 590 ms 227876 KiB
04_star_01.txt AC 524 ms 188916 KiB
04_star_02.txt AC 761 ms 223280 KiB
05_uni_00.txt AC 403 ms 118880 KiB
05_uni_01.txt AC 606 ms 173156 KiB
05_uni_02.txt AC 817 ms 218580 KiB
05_uni_03.txt AC 869 ms 218284 KiB
05_uni_04.txt AC 895 ms 224436 KiB
05_uni_05.txt AC 907 ms 224432 KiB
06_bi-ternary_00.txt AC 824 ms 222712 KiB
06_bi-ternary_01.txt AC 710 ms 221424 KiB
06_bi-ternary_02.txt AC 344 ms 118880 KiB
06_bi-ternary_03.txt AC 814 ms 223344 KiB
06_bi-ternary_04.txt AC 748 ms 222164 KiB
06_bi-ternary_05.txt AC 354 ms 119232 KiB
06_bi-ternary_06.txt AC 813 ms 222068 KiB
06_bi-ternary_07.txt AC 756 ms 222956 KiB
06_bi-ternary_08.txt AC 353 ms 119108 KiB
06_bi-ternary_09.txt AC 828 ms 224856 KiB
06_bi-ternary_10.txt AC 746 ms 221160 KiB
06_bi-ternary_11.txt AC 379 ms 120148 KiB