Hello I am trying to solve simple graph algorithms problem counting room. In this problem I am getting runtime and time limit exceeded if vector is not defined globally. Can someone tell why this is happening.
Problem link - Counting rooms
Accepted code - Accepted code TLE and RE showing code - Rejected code
My approach- I am calling dfs function for every unvisited cell and coloring the visited cell.
The number of times dfs is called will be the answer.
CodePudding user response:
In the Rejected code you are passing vector by value which is making copies of the the vector again and again.
CodePudding user response:
Pass vector by reference i.e. simply put '&' sign before vector in arguments of dfs function
CodePudding user response:
in the rejected code when you are calling the function it is making a new copy of the vector which takes o(size of(vector)), which is expensive instead of that u can define the vector as a global vector and use it further