1971. Find if Path Exists in Graph

PHOTO EMBED

Sat Mar 18 2023 17:10:34 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

class Solution {
public:
    bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
        unordered_map<int, set<int>>m;
        vector<bool> visited(n, false);
        visited[destination]=0;
        for(int i=0;i<edges.size();i++)
        {
            int u=edges[i][0];
            int v=edges[i][1];
            m[u].insert(v);
            m[v].insert(u);
        }
        queue<int>q;
        q.push(source);
        visited[source]=true;
        while(!q.empty())
        {
            int p=q.front();
            q.pop();
            
            
            for(auto x:m[p])
            {
                if(!visited[x])
                {
                    q.push(x);
                    visited[x]=true;
                }
            }
        }
        if(visited[destination]==1) return true;
        return false;
    }
};
content_copyCOPY

https://leetcode.com/problems/find-if-path-exists-in-graph/