Comments

The same approach I used during testing but it is wrong.

As a first-fooled person, I suggest everyone participate in this contest.

0

https://cp-algorithms.com/graph/tree_painting.html

You can also refer this blog, small modifications in given code will also work.

Yes.

The number of elements in the answer is actually 3, but your code gives 2.

5

3 4 1 5 4

For this test case, your code gives wrong answer.

Problem C and 923B - Producing Snow are same.

Use Fast IO