Find the nearest clone


Problem Statement :


In this challenge, there is a connected undirected graph where each of the nodes is a color. Given a color, find the shortest path connecting any two nodes of that color. Each edge has a weight of . If there is not a pair or if the color is not found, print -1.


Function Description

Complete the findShortest function in the editor below. It should return an integer representing the length of the shortest path between two nodes of the same color, or -1  if it is not possible.

findShortest has the following parameter(s):

g_nodes: an integer, the number of nodes
g_from: an array of integers, the start nodes for each edge
g_to: an array of integers, the end nodes for each edge
ids: an array of integers, the color id per node
val: an integer, the id of the color to match



Input Format

The first line contains two space-separated integers n and m, the number of nodes and edges in the graph.
Each of the next m lines contains two space-separated integers g_from[ i ] and g_to[ i ] , the nodes connected by an edge.
The next line contains n space-seperated integers, ids[ i ], representing the color id of each node from  1 to n.

The last line contains the id of the color to analyze.

Note: The nodes are indexed from 1 to n.


Constraints


1   <=   n  <=  10^6
1   <=   m  <=  10^6
1   <=   ids[ i ]  <=  10^8


Output Format

Print the single integer representing the smallest path length or -1.



Solution :



title-img




                        Solution in C++ :

In   C ++ :






#include <bits/stdc++.h>
#define MAX 1000003
using namespace std;

vector<int> v[MAX];
bool visit[MAX];
int a[MAX],c,id;

void bfs(int i)
{
    memset(visit, false, sizeof visit);
    queue<pair<int,int> > q;
    pair<int,int> p;
    q.push({i,0});
    visit[i]=true;

    while(!q.empty()) {
        p=q.front();
        q.pop();
        for(auto x: v[p.first]) {
            if(!visit[x]) {
                if(a[x] == id) {
                    c=p.second+1;
                    return;
                }
                visit[x]=true;
                q.push({x,p.second+1});
            }
        }
    } 
}

int main()
{
    int n,m,i,x,y;
    cin>>n>>m;

    for(i=0;i<m;++i) {
        cin>>x>>y;
        x-=1,y-=1;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    for(i=0;i<n;++i) {
        cin>>a[i];
    }
    cin>>id;

    int ans=1e9;
    for(i=0;i<n;++i) {
        if(a[i]==id) {
            c=1e9;
            bfs(i);
            ans=min(ans, c);
        }
    }
    if(ans==1e9) {
        ans=-1;
    }
    cout<<ans<<endl;

    return 0;
}
                    






View More Similar Problems

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →