Please can someone help me this problem :
https://www.spoj.com/problems/BUGLIFE/
I am getting wrong answer when I m changing the color explicitly i.e before calling dfs here is the code : Your text to link here...
And i got AC when I send the color along with dfs function Your text to link here...
if u look at both the codes dont u think they are equivalent? If not please tell me which edge case am i missing? Thanks in advance :)
For easy test:
There you do recursion from 1. You are coloring node 1 with color 0. For neighbor 2 you change color to 1. For neighbor 3 you change color back to zero. There you recognize error although there is none.
Thanku very much for ur help :). Well is it possible to do this explicitly without passing into dfs? But u know passing into dfs is better :)
You can use
c ^= 1;
as very first command in dfs and as last command beforereturn true;
(Additionally removec ^= 1;
anywhere else). Then I guess it would be fine. (You can think of giving each node the colordepth & 1
and incrementing/decrementing the depth at beginning/end)