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; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter