Dfs question unable to understand

https://codeforces.com/contest/1118/problem/F1

In this question i used bfs and it is showing tle bacuse my sol time complexity is o(n^3) showing TLE in test case 12

but i am unable to understand editorial solution
please help!!!

@akshay2229 this problem can be done in O(n) as follows:
make a array of pairs and for every node store how many red and blue vertices are in its subtree by running a dfs taking 1 as the root node
then for every node starting from i=2 there will be a nice edge if subtree under that node should not contain both type of colours and if it contains any one of the colour it should be the total colour present in the tree so when you cut from here all colours of that type remain in that subtree only.
check my solution (https://ide.codingblocks.com/s/215228)

i have some doubt in code

yeah tell me where you have doubt