Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
For each node, define a <code>flag[v]</code> indicating that the subtree of this node contains only one color or not.
In the DFS process, when you call <code>dfs(u)</code> from node <code>v</code>, after that DFS of <code>u</code> has finished, check if <code>flag[u] == false</code>, then <code>flag[v]</code> is also <code>false</code>.
Also if <code>color[v] != color[u]</code>, <code>flag[v]</code> becomes <code>false</code>.