# Issues with my graph theory solution(easy)

Hello guys,I was doing this graph theory problem which requires simple bfs.I have few issues with this hope you can help me out.

P.S-I have seen its solution on web and I cannot understand why we are doing bfs from every node?? And summing maximum of red and black everytime.Please see my solution v/s one i found in web.Thank you

The error in your code is that you are not marking your initial x as 1. Do, vis[x]=1 before starting your bfs traversal. It should possibly work after this.

The input you are given is not in any necessary sequential order. I.e consider 3 fights between where u and v are:

``````4 5
1 2
2 3
``````

How are you supposed to reach 4 in this bfs traversal of yours? Your code will start from 2. Reach 3 and 1 and will give 2 as the answer. Thereâ€™s no way you reached for the fight between 4 and 5. So, for this purpose, we keep a loop in main() this manner:

``````for(int i=1;i<=max_n;i++){
if(color[i]== INVALID_ENTRY)
{
// this signifies that the node i has not been included in any bfs traversal till now
bfs(i);
}
else if(color[i]==1 || color[i]==0)
continue;
// as valid entry as per your code is 0 or 1. So, keep something like -1 for invalid color and identifying that the nde has not been visited.
}
``````

However, you can simply do a `if(vis[i]==0)` check instead of the color check above.

1 Like