You are not logged in. Please login at to post your questions!


Longest Path in an undirected tree

I used two DFS to approach the solution. But I'm getting run time error. Can anyone help me with this? Link to the problem

Link to source code

asked 16 Feb, 20:18

spd123's gravatar image

accept rate: 0%

Your solution is accepted after correcting some logical error.

there were some logical errors in your solution which were giving Segfault.

int *dist;

Here dist is an integer pointer so sizeof(dist) will give either $8$ byte or $4$ byte depending on architecture.

So the line


is making all the worst. Because it is setting only first 4 or 8 bytes as -1 but the array size is larger than this. Now if you replace this with

memset(dist,-1,sizeof(int) * (N + 1));

then this will work.

Edited code link LongestPathCorrected


answered 17 Feb, 15:05

brij_raj's gravatar image

accept rate: 10%

edited 17 Feb, 16:20

Thanks @brij_raj

(18 Feb, 11:48) spd1232★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 16 Feb, 20:18

question was seen: 115 times

last updated: 18 Feb, 11:48