/ SeriousOJ /

Record Detail

Runtime Error


  
# Status Time Cost Memory Cost
#1 Accepted 52ms 17.402 MiB
#2 Accepted 33ms 17.434 MiB
#3 Accepted 33ms 17.406 MiB
#4 Accepted 35ms 17.293 MiB
#5 Accepted 281ms 88.555 MiB
#6 Accepted 288ms 87.535 MiB
#7 Accepted 348ms 87.715 MiB
#8 Accepted 380ms 88.648 MiB
#9 Accepted 445ms 87.707 MiB
#10 Accepted 406ms 87.613 MiB
#11 Accepted 430ms 87.652 MiB
#12 Accepted 404ms 87.578 MiB
#13 Accepted 533ms 82.992 MiB
#14 Accepted 547ms 82.934 MiB
#15 Accepted 559ms 82.773 MiB
#16 Accepted 546ms 83.008 MiB
#17 Accepted 562ms 82.906 MiB
#18 Accepted 571ms 82.91 MiB
#19 Accepted 265ms 81.703 MiB
#20 Runtime Error Traceback (most recent call last): File "foo.py", line 66, in <module> File "foo.py", line 29, in dfs File "foo.py", line 29, in dfs File "foo.py", line 29, in dfs [Previous line repeated 1497 more times] File "foo.py", line 28, in dfs RecursionError: maximum recursion depth exceeded 266ms 86.469 MiB
#21 Runtime Error Traceback (most recent call last): File "foo.py", line 66, in <module> File "foo.py", line 29, in dfs File "foo.py", line 29, in dfs File "foo.py", line 29, in dfs [Previous line repeated 1684 more times] File "foo.py", line 28, in dfs RecursionError: maximum recursion depth exceeded 264ms 85.328 MiB

Code

#Kamonasish Roy : Python code

def dfs1(x, p):
    for it in edge[x]:
        if it != p:
            up[it] = up[x] + 1
            if x == 1:
                low[it] = dp[x][0]
                if dp[x][0] == level[it] + 1:
                    low[it] = dp[x][1]
                low[it] -= 1
                low[it] = max(low[it], 0)
            else:
                low[it] = low[x]
                cur = dp[x][0]
                if dp[x][0] == level[it] + 1:
                    cur = dp[x][1]
                cur -= 1
                cur = max(0, cur)
                bonus[it] = bonus[x]
                bonus[it] += cur
                global mx
                mx = max(mx, up[it] + min(low[x] + bonus[it], k))
            dfs1(it, x)

def dfs(x, p):
    for it in edge[x]:
        if it != p:
            dfs(it, x)
    level[p] = max(level[p], level[x] + 1)
    if dp[p][0] < level[p]:
        dp[p][0], dp[p][1] = level[p], dp[p][0]
    elif level[x] + 1 > dp[p][1]:
        dp[p][1] = level[x] + 1

if __name__ == "__main__":
    import sys
    input = sys.stdin.read
    data = input().split()
    
    idx = 0
    t = 1  
    results = []
    
    for _ in range(t):
        n = int(data[idx])
        k = int(data[idx + 1])
        idx += 2
        
        global level, dp, low, up, bonus, edge, mx
        level = [1] * (n + 1)
        dp = [[0, 0] for _ in range(n + 1)]
        low = [0] * (n + 1)
        up = [0] * (n + 1)
        bonus = [0] * (n + 1)
        edge = [[] for _ in range(n + 1)]
        
        for i in range(1, n):
            x = int(data[idx])
            y = int(data[idx + 1])
            idx += 2
            edge[x].append(y)
            edge[y].append(x)

        dp[1][0] = 1
        dfs(1, 0)
        mx = dp[1][0]
        up[1] = 1
        mx += min(max(0, dp[1][1] - 1), k)
        dfs1(1, 0)
        results.append(min(mx, n))
    
    print("\n".join(map(str, results)))

Information

Submit By
Type
Submission
Problem
P1111 Thakurs tree game
Language
PyPy 3 (Python 3.9.18 PyPy 7.3.15)
Submit At
2024-10-21 22:49:51
Judged At
2024-10-21 22:53:35
Judged By
Score
90
Total Time
571ms
Peak Memory
88.648 MiB