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

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

Thanks @brij_raj