• + 1 comment

    what is wrong with this code...gives timeout error after test case 9...I did BFS before processing the query and then taking LCA of each query..finally finds

    #include<iostream>
    #include <list>
    #include<vector>
    #include<queue>
    using namespace std;
    
    void BFS(int s,int* parent,bool* visited,int N,vector<vector<int> >& pairs,int* depth)
    {
        visited[s] = true;
        depth[s] = 0;
        queue<int> q;q.push(s);
        while(!q.empty())
        {
            int s = q.front();
            q.pop();
            vector<int>::iterator i;
            for(i=pairs[s].begin();i!=pairs[s].end();i++)
            {
                if(!visited[*i])
                {
                    visited[*i] = true;
                    q.push(*i);
                    parent[*i] = s;
                    depth[*i] = depth[s]+1;
                }
            }
        }
    }
    int findLCA(int u,int v,int* parent)
    {
        if(u==v)    return u;
        else return findLCA(parent[u],parent[v],parent);
    }
    int gcd(int a,int b){
        return (b==0) ? a : gcd(b, a%b);
      }
    int countPairs(queue<int>& q,int* nodeV)
    {
        vector<int> arr;
        int k=0;
        while(!q.empty())
        {
            arr.push_back(nodeV[q.front()]);
            q.pop();
            k++;
        }int count = 0;
        for(int i=0;i<k-1;i++)
        {
            for(int j=i+1;j<k;j++)
            {
                if(gcd(arr[i],arr[j]) == 1) count++;
            }
        }
        return count;
    }
    int main(){
        int N,q;cin>>N>>q;
        int* nodeV = new int[N];
        for(int i=0;i<N;i++)    cin>>nodeV[i];
        vector<vector<int> >pairs(N);
        for(int i=0;i<N-1;i++)
        {
            int u,v;cin>>u>>v;
            pairs[u-1].push_back(v-1);
            pairs[v-1].push_back(u-1);
        }
        int* parent = new int[N];
        int* depth = new int[N];
        bool* visited = new bool[N];
        for(int i=0;i<N;i++)
        {
            parent[i] = -1;depth[i] = -1;
            visited[i] = false;
        }
        int s=0;
        BFS(s,parent,visited,N,pairs,depth);
        //for(int i=0;i<N;i++)    cout<<parent[i]<<" ";
        for(int i=0;i<q;i++)
        {
            int u,v;cin>>u>>v;u=u-1;v=v-1;
            int k1 = depth[u];
            int k2 = depth[v];
            queue<int> q;
            while(k1!=k2)
            {
                if(k1>k2)
                {
                    q.push(u);
                    u = parent[u];k1--;
                }
                else
                {
                    q.push(v);
                    v = parent[v];k2--;
                }
            }
            if(u==v && q.empty()==true)
            {
                cout<<"0\n";
            }
            else
            {
                int lca = findLCA(u,v,parent);
                while(u!=lca)
                {
                    q.push(u);
                    u = parent[u];
            }
            while(v!=lca)
            {
                q.push(v);
                v = parent[v];
            }
            q.push(lca);
            int total = countPairs(q,nodeV);
            cout<<total<<"\n";
            }
        }
        return 0;
    }
    

    the path.