HELP RISK div3

https://www.codechef.com/viewsolution/45426905
why i am getting tle.?
my code looks same as editorialist.
Please help.

Just saw the signature of dfs:

ll dfs(int x,int y,vector<string> v, vector<vector<int>> &vis)

Try putting vector<string> &v instead.

it worked. thanks.
but can u explain how is it causing the affect.

I think that’s because multiple different copies of v are getting generated.

When You are sending it like
ie pass by value
vector<string> v
its one more copy get created and that copy is used in the function and this copy is destroyed when you return from a function
but When you pass it by refernce means
vector<string> &v
this way doesn’t create new copy instead it uses this vector from a place where it was created for the first time
And for creation of each copy it takes O(n) time
which is saved in case of pass by reference