0

I tried solving a challenge that asks for the diameter of a tree (not necessarily a binary tree) in python 3.0. The challenge is actually to find the "circumference" of a tree, which is basically the diameter of a tree (length of the longest path) multiplied by 3.

I created a adjacency list and ran depth-first search (DFS) on the tree, storing the greatest two heights of the sub-trees, and the maximum diameter encountered so far. I think my logic is pretty correct, but I can't seem to find a problem with the code.

The codeforces compiler gave this error:

"memory limit exceeded on test case 1" for PyPy 3.6
"Runtime error on test case 4" for Python 3.7

But I could not find any test case myself for which the code produced an error.

Problem reference: https://codeforces.com/gym/102694/problem/A

My code:

import sys,math
sys.setrecursionlimit(10**7)
def get_int(): # returns a list of integers after taking an input
    return list(map(int,sys.stdin.readline().strip().split()))

def dfs(adj_list,u):
    global visited
    d,h=0,[0,0] #Diameter, Maximum two heights.
    # print(u)
    if adj_list[u]==[]:
        # print(0,0)
        return 0,0
    else:
        for v in adj_list[u]:
            if visited[v]==True:
                continue
            else:
                visited[v]=True
                tmp_d,tmp_h=dfs(adj_list,v)
                d=max(d,tmp_d)
                tmp_h+=1
                if tmp_h>h[1]:
                    if tmp_h>h[0]:
                        h[1],h[0]=h[0],h[1]
                        h[0]=tmp_h
                    else:
                        h[1]=tmp_h
        d=max(h[0]+h[1],d)
        # print(d,h[0])
        return d,h[0]

if __name__ == "__main__":
    N=int(input().strip())
    adj_list=[[] for _ in range(N+1)] #Stores the adjacency list
    adj_list[0]=None
    root=1
    for _ in range(N-1):
        a,b=get_int()
        adj_list[a].append(b) 
        adj_list[b].append(a)
    visited=[False]*(N+1)
    visited[1]=True
    print(3*dfs(adj_list,root)[0])   
  • Welcome. Please update your question: code should not be posted as an image. Type the code in your question and format it with the tool button. If you get a runtime error, you should at least mention the specifics of that error, and the line in which it occurred (stack trace). There should not be any essential information behind a link. – trincot Sep 01 '20 at 21:39
  • I have posted the code itself as you asked, but I can't pinpoint the line which throws the error because it worked with the TCs that I came up with. – Amartya Ghosh Sep 02 '20 at 03:22
  • You should add the description of the task in the question. It should not be behind a link. Essential is also: what is the definition of the diameter? The most common definition is the number of nodes on the longest path, yet your code seems to count the number of edges. Also, why multiply with 3? This is not clear from your question. Also, do not use abbreviations for "test case" of "Non zero exit code". The latter is even too vague to help anyone. Mention the exact error message. – trincot Sep 02 '20 at 07:58
  • The problem asks you to find the circumference of a tree which is basically diameter of a tree(length of the longest path) multiplied by 3. To answer your second question, all that the codeforces compiler showed was : ("memory limited exceeded on test case 1" for PyPy 3.6) and (""Runtime error on test case 4" for Python 3.7) and I can't be any more exact than that because the compiler doesn't show that exact error message. – Amartya Ghosh Sep 02 '20 at 11:38
  • I think you did not copy the error message right: "memory limited exceeded" could be "memory limit exceeded". Can you be exact, and edit the question (I already did that for you, but please correct where necessary). – trincot Sep 02 '20 at 11:49

0 Answers0