I was doing for 50 points using DFS and keeping track of distance of each node from a node. But I don’t know why it is giving WA.

Code is here

Thanks in advance!!

Edit: Could not find editorial.

1 Like

Anyone there?? :joy: who can help!

Line 11 : Insanely large 2d array ? Really ? I hope you know about the max allowed size of global array.

Line 35, 39, 40 : I don’t understand what are you trying to do here, but most probably, you are assuming the given tree to be a binary tree ( which is no where mentioned ). Take examples of tree having 3-5 children, and I guess, you will notice how these lines are messing up.


Thanks for replying but the lines 35, 39, 40 are basically to store the distance of a node from each node.
I took large 2D array because I was thinking if there is problem with array!!:sweat_smile:

10^8 size works with Codechef.

1 Like

You forgot to intialize visited array. I think that might be an issue.


I have initialized in line 19.

Wtf you are not intializing it. You are just declaring the array. Intialize them to 0.
bool visited[n+1]={0}
Or use a for loop to make all of them zero intially.
Also I found some basic critical issues in your code.
dist should be updates b/w current node and root ( i.e. i in your case) .
I will edit it soon and will give link to an AC submission here.

1 Like

But after declaring everytime it all clear the previous?

Are you using c for the first time ?
It will contain garbage values when you declare it. Unlike some other languages c/c++ do not intialize array automatically.
You need to make all of them zero yourself if required. Otherwise it’s value will depend on memory allocated. It will have some random values in short.


okay…i’ll fix this.


I am still getting WA…

There was an issue in the bfs.
Line 40-41 will assign different levels to nodes in same level.
Here is the code for 50 points after some modification.


Okay…got it! Thanks