/ SeriousOJ /

Record Detail

Runtime Error


  
# Status Time Cost Memory Cost
#1 Accepted 40ms 17.359 MiB
#2 Accepted 37ms 17.27 MiB
#3 Accepted 38ms 17.242 MiB
#4 Accepted 38ms 17.336 MiB
#5 Accepted 278ms 87.031 MiB
#6 Accepted 290ms 87.379 MiB
#7 Accepted 373ms 87.875 MiB
#8 Accepted 425ms 88.707 MiB
#9 Accepted 476ms 87.688 MiB
#10 Accepted 442ms 87.5 MiB
#11 Accepted 441ms 87.656 MiB
#12 Accepted 442ms 87.336 MiB
#13 Accepted 592ms 82.992 MiB
#14 Accepted 623ms 82.914 MiB
#15 Accepted 589ms 82.785 MiB
#16 Accepted 590ms 83.027 MiB
#17 Accepted 605ms 83.004 MiB
#18 Accepted 601ms 82.914 MiB
#19 Accepted 308ms 81.586 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 304ms 85.621 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 294ms 85.074 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-11-11 02:36:03
Judged By
Score
90
Total Time
623ms
Peak Memory
88.707 MiB