Find the nearest clone

  • + 1 comment

    Passed all cases but these test cases seem very weak. Anybody with some solid test cases?

    int bfs(vector<int> adj[], int start, int n, vector<long> ids, int val){
        queue<int> q;
        vector<bool> visited(n , false);
        vector<int> level(n, -1);
        q.push(start);
        visited[start] = true;
        level[start] = 0;
        
    
        while(!q.empty()){
            int s = q.front();
            q.pop();
    
            
                for(auto x : adj[s]){
                    if(!visited[x]){
                        q.push(x);
                        visited[x] = true;
                        level[x] = level[s] + 1;
                        if(ids[x] == val) return level[x];
    
                    }
                }
            
        }
    
        return -1;
    }
    
    int findShortest(int graph_nodes, vector<int> graph_from, vector<int> graph_to, vector<long> ids, int val) {
        
        vector<int> adj[graph_nodes];
    
        for(int i = 0; i < graph_from.size(); i++){
            int u = graph_from[i] - 1;
            int v = graph_to[i] - 1;
    
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
    
       
        
        int min = INT_MAX;
    
        for(int i = 0; i < graph_nodes; i++){
            if(ids[i] == val){
                int cur = bfs(adj, i, graph_nodes, ids, val);
                if(min > cur && cur != -1) min = cur;
            }
        }
    
        if(min == INT_MAX) min = -1;
        return min;
        
    }