Why my program gives timeout when its kinda exactly same to other one

here is my code which is giving timeout

here is other code which got accepted

the only noticeable difference I can see is he is using global variables and I am using local ones so does it make any difference as I already know in the precomputation case it does but in this code we are not precomputing still I get TLE what is the catch here please explain someone Thanks in advance.

using namespace std;


const unsigned int M = 1000000007;

void graphDFScomponentcounter(int node,vector<vector<int>> v, vector<bool> &vmarked,int &counter ){ // The bug is here
    vmarked[node]=true;
    counter++;
    fri(i,0,v[node].size()){
        if (!vmarked[v[node][i]]){
            graphDFScomponentcounter(v[node][i],v,vmarked,counter);
        }
    }
}

The reason behind TLE is the method signature.

It should have been
void graphDFScomponentcounter(int node,vector<vector<int>> &v, vector<bool>&vmarked,int &counter )
Instead

(I guess)

2 Likes

didn’t get your point bro can you please elaborate a little ?

This explains everything you need.

2 Likes

He means to use call by reference instead of call by value.

2 Likes

thanks @suman_18733097 brother and @linga_abhishek :smiley: got your point and yea I was making that mistake of passing adj list not by reference

This similar type of problem I faced in codeforces recent div2 round C ques. When passing by reference it was accepted when not it showed TLE.

1 Like