id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\nprivate: \n bool check; \n int node; \npublic:\n\n void dfs( int root, vector<bool>& visited, vector<int>& edges, vector<int>& result )\n {\n if( root == node )\n check = true; \n visited[root] = true; \n result.push_back( root );\n\n if(... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n=edges.size();\n vector<int> adj[n];\n for(int i=0;i<n;i++)\n {\n if(edges[i]!=-1) adj[i].push_back(edges[i]);\n }\n queue<pair<int,int>> q;\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int m = edges.size();\n vector<vector<int>> g(m+1);\n for(int i=0;i<m;++i)\n {\n if(edges[i] != -1)\n {\n g[i].push_back(edges[i]);\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "#define ll long long int\n#define endl '\\n'\n#define pb push_back\n#define pob pop_back\n#define mp make_pair\n#define ff first\n#define ss second\n#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)\nusing namespace std;\nconst int MOD = 1000000007; // 10^9 + 7\nconst int INF... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n = edges.size();\n\n vector<vector<int>> adj(n);\n for(int i=0 ; i<n ; i++) {\n if(edges[i] != -1) {\n adj[i].push_back(edges[i]);\n }\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n\nvoid dijsktra(vector<int>adjList[],vector<int>& dis,int node,int flag,vector<int> &vis){\n vis[node]=1;\n dis[node]=flag;\n\n for(auto &it:adjList[node]){\n if(!vis[it]){\n dijsktra(adjList,dis,it,flag+1,vis);\n }\n }\n}\n int closestMeet... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n unordered_map<int,int> n1;\n n1[node1] = 0;\n int lastN1 = node1;\n while(edges[lastN1] > -1 && ! n1.count(edges[lastN1])){\n n1[edges[lastN1]] = n1[lastN1]+1;\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n void shdt(int nd, vector<int> &dt, vector<vector<int>> &adj)\n {\n dt[nd]=0;\n queue<pair<int, int>> q;\n q.push({0, nd});\n while(!q.empty())\n {\n auto [wt, nd1]=q.front();\n q.pop();\n for(auto it: adj[... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n vector<int> bfs(vector<vector<int>>& g, int v, int n) {\n vector<int> dist(n, 1e9);\n dist[v] = 0;\n\n queue<pair<int, int>> q;\n q.push({0, v});\n\n while (!q.empty()) {\n auto p = q.front();\n q.pop();\n\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n vector<int>dijkstra(vector<vector<pair<int,int>>>&adj,int node){\n int n=adj.size();\n vector<int>vis(n,false);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>pq;\n pq.push({0,node});\n vector<int>dis(n,INT_MAX);\n dis... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int a, int b) {\n int n = edges.size();\n int dis1[n+1];int dis2[n+1];\n for(int i =0 ;i<n;i++)\n {\n dis1[i]=-1;\n dis2[i]=-1;\n }\n map<int,int> mp;\n for(int i... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n = edges.size();\n vector<int> g[n];\n for (int i = 0; i < n; ++i) {\n if (edges[i] != -1) {\n g[i].push_back(edges[i]);\n }\n }\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n=edges.size();\n vector<int> adj[n];\n \n for(int i=0;i<n;i++){\n if(edges[i]!=-1)\n adj[i].push_back(edges[i]);\n }\n\n vector<int> dist(... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\n void dfs(int node, vector<vector<int>> &graph, vector<int> &dis){\n for(auto c : graph[node]){\n if(dis[c]==-1){\n dis[c] = dis[node]+1;\n dfs(c,graph,dis);\n }\n }\n }\npublic:\n int closestMeetingNode(vector<int... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n\n void bfs(vector<vector<int>> &adj,int sv,vector<int> &dst,int n){\n vector<bool> vis(n,false);\n queue<pair<int,int>> q;\n q.push({sv,0});\n vis[sv]=true;\n while(!q.empty()){\n int node=q.front().first;\n int lvl=q.f... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n\n void bfs (int n , int node , vector<vector<int>> &adj , vector<int> &dd , vector<int> &vis){\n vis[node] = 1;\n for(int i =0;i<adj[node].size();i++){\n int temp = adj[node][i];\n if(vis[temp]==-1){\n dd[temp] = dd[node]+1;\... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n\n void bfs (int n , int node , vector<vector<int>> &adj , vector<int> &dd , vector<int> &vis){\n vis[node] = 1;\n for(int i =0;i<adj[node].size();i++){\n int temp = adj[node][i];\n if(vis[temp]==-1){\n dd[temp] = dd[node]+1;\... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n vector <int> dijkstra(int V, vector<int> adj[], int S)\n {\n set<pair<int,int>> st; \n vector<int> dist(V, 1e9); \n st.insert({0, S}); \n dist[S] = 0;\n while(!st.empty()) {\n auto it = *(st.begin()); \n int node = i... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n #define ll long long int\n int closestMeetingNode(vector<int>& e, int node1, int node2) {\n int n=e.size();\n vector<int>adj[n];\n for(int i=0;i<n;i++)\n {\n if(e[i]!=-1)\n {\n adj[i].push_back(e[i]);\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n vector<int> adj[edges.size()];\n for(auto i = 0 ;i<edges.size();i++){\n if(edges[i]!=-1)\n adj[i].push_back(edges[i]);\n }\n int n = edges.size();\n v... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int e = 0;\n map<int, int, greater<int>> bfs(vector<int> adj[], int src){\n map<int, int, greater<int>> mp;\n queue<int> q;\n vector<int> seen(e, 0);\n int dist = 1;\n mp.insert({src, 0});\n q.push(src);\n while (!q.empty())... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n \n \n static bool cmp(pair<int,int>&a,pair<int,int>&b)\n {\n if(a.first!=b.first)return a.first<b.first;\n else return a.second<b.second;\n }\n void bfs(int node,vector<int>g[],vector<int>&vis,unordered_map<int,int>&m1)\n {\n queue<pair<i... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n vector<vector<int>> adj(edges.size());\n for (int i = 0; i < edges.size(); i++) {\n if (edges[i] != -1)\n adj[i].push_back(edges[i]);\n }\n \n vec... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n unordered_map<int,int>graph;\n for(int i=0;i<edges.size();i++)\n {\n if(edges[i]!=-1)\n graph[i]=edges[i];\n }\n unordered_map<int,int>mapping1;\... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int N=edges.size();\n vector<int>adj[N];\n for(int i=0;i<edges.size();i++)\n {\n if(edges[i]!=-1)\n {\n adj[i].push_back(edges[i]);\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n void dfs(int src, vector<bool>& vis, vector<vector<int>>& adj, vector<int>& dis) {\n vis[src] = true;\n\n for (auto i : adj[src]) {\n if (!vis[i]) {\n dis[i] = dis[src] + 1;\n dfs(i, vis, adj, dis);\n }\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int n;\n void dfs(int node, vector<vector<int>> &adj, vector<int>&dist, vector<bool> &visited)\n {\n visited[node]=true;\n\n for(auto &it : adj[node])\n {\n if(!visited[it])\n {\n dist[it] = dist[node]+1;\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<int>>&adj, int u, vector<bool>&vis, vector<int>&dis){\n vis[u] = true;\n for (auto &v : adj[u]){\n if (!vis[v]){\n dis[v] = dis[u]+1;\n dfs(adj, v, vis, dis);\n }\n }\n }\n i... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "\nclass Solution {\n void dfs(vector<vector<int>>& graph, vector<int> &mp, vector<bool>& vis, int node, int w){\n mp[node] = w;\n vis[node] = true;\n for(int i = 0; i < graph[node].size(); i++){\n int child = graph[node][i];\n if(vis[child] == false || w + 1 < ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "\nclass Solution {\n void dfs(vector<vector<int>>& graph, vector<int> &mp, vector<bool>& vis, int node, int w){\n mp[node] = w;\n vis[node] = true;\n for(int i = 0; i < graph[node].size(); i++){\n int child = graph[node][i];\n if(vis[child] == false || w + 1 < ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\n\nprivate:\n void dfs(int u, vector<int>& edges, unordered_map<int,int> &from, unordered_set<int> &recStack) {\n\n recStack.insert(u);\n\n if(edges[u] != -1 && recStack.find(edges[u]) == recStack.end()) {\n from[edges[u]] = from[u] + 1;\n\n dfs(edges... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n\nint n,m;\n vector<vector<int>>g;\n\n vector<int>rec(int node)\n {\n vector<int>dist(g.size()+3,-1),visited(g.size()+3,0);\n list<int>dp;\n dist[node]=0;\n dp.emplace_back(node);\n\n while(dp.size()>0)\n {\n auto it=dp.front();\n visited[it]=1;\n\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> adj; \n vector<int> shortestpath(int node){\n int n = adj.size();\n vector<int> dist(n,1e8);\n multiset<pair<int,int>> ms;ms.insert({0,node});\n dist[node] = 0;\n while(!ms.empty()){\n int node = ms.begin()-... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n int n = edges.size();\n vector<vector<int>>adj(n);\n for(int i=0; i<n; i++){\n if(edges[i] != -1)\n adj[i].push_back(edges[i]);\n }\n vector<int>dist1... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\nvector<vector<int>>g;\n\nvector<int> f(int S , int nn){\n set<pair<int ,int>> st;\n vector<int> dist(nn,1e9);\n dist[S]=0;\n st.insert({dist[S],S});\n while(!st.empty()){\n auto top = *(st.begin());\n int N = top.second;\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\n // Function for Depth First Search\n void dfs(int i, vector<bool>& visited, vector<vector<int>>& adj, vector<int>& distance_node1, int dist)\n {\n // Marking the node visited\n visited[i]=true;\n // Storing the distance \n distance_node1[i]=dist;\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n vector<int>adj[100005];\n for(int i=0;i<edges.size();i++){\n if(edges[i]!=-1){\n adj[i].push_back(edges[i]);}\n }\n priority_queue<pair<int,int>, vector<pair... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n int closestMeetingNode(vector<int>& edges, int node1, int node2) {\n //Idea is to run bfs starting from both nodes, and then store their distances \n //In 2 separate hashmaps, and compare the minimum of the (x1, x2) of all common nodes \n int n = edges.si... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n void dfs(unordered_map<int, int>& mp, int node, vector<int>& dist, vector<bool>& vis) {\n vis[node] = true;\n int child = mp[node];\n if (child != -1 && !vis[child]) {\n vis[child] = true;\n dist[child] = 1 + dist[node];\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n vector<int>solve(vector<int>adj[],int src,int n){\n vector<int>dis(n,1e9);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n dis[src]=0;\n pq.push({0,src});\n while(!pq.empty()){\n int front= pq.top... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\npublic:\n\n void bfs(int i,vector<vector<int>>& adj,vector<int>& dis,vector<int>& vis)\n {\n dis[i]=0;\n queue<int> q;\n q.push(i);\n while(!q.empty())\n {\n int node=q.front();\n q.pop();\n if(vis[node]==1)\n ... |
2,438 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 3 | {
"code": "class Solution {\n void dijkastra(int i,vector<int>&dist,unordered_map<int,vector<pair<int,int>>>&adj){\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n dist[i] = 0;\n pq.push({0,i});\n while(!pq.empty()){\n auto t = pq.top();\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\n int traverseAndFindCycleLen(vector<int> & edges, int i) {\n int n = edges.size();\n int res = -1;\n\n int slowPtr = i, fastPtr = i;\n while (edges[fastPtr] != -1 && edges[edges[fastPtr]] != -1) {\n slowPtr = edges[slowPtr];\n fastPtr =... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\n int traverseAndFindCycleLen(vector<int> & edges, int i) {\n int n = edges.size();\n int res = -1;\n\n // Using Floyds cycle detection algorithm\n int slowPtr = i, fastPtr = i;\n while (edges[fastPtr] != -1 && edges[edges[fastPtr]] != -1) {\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size();\n vector<int> rank(n, -1);\n int currank = 0;\n int maxlength = -1;\n for (int i = 0; i < n; ++i) {\n if (rank[i] == -1) {\n rank[i] = currank++;\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\n public:\n int longestCycle(vector<int>& edges) {\n int ans = -1;\n int time = 1;\n vector<int> timeVisited(edges.size());\n\n for (int i = 0; i < edges.size(); ++i) {\n if (timeVisited[i])\n continue;\n const int startTime = time;\n int u = i;\n wh... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\n public:\n int longestCycle(vector<int>& edges) {\n int ans = -1;\n int time = 1;\n vector<int> timeVisited(edges.size());\n\n for (int i = 0; i < edges.size(); ++i) {\n if (timeVisited[i])\n continue;\n const int startTime = time;\n int u = i;\n wh... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size();\n vector<bool> visited(n, false); // To mark if a node is already fully processed\n vector<int> visitTime(n, -1); // To store the time when a node was first visited\n int timer = ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution\n{\npublic:\n int longestCycle(vector<int>& edges)\n {\n int V = edges.size();\n vector<int> indegree(V, 0); // Store in-degree of each node\n\n // Step 1: Build the graph and in-degree array\n for (int i = 0; i < V; ++i)\n {\n if (edges[i... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\npublic:\n void dfs(int node, vector<int> &edges, vector<int> &indegrees, int &count){\n\n if (indegrees[node] <= 0) \n return; \n indegrees[node]--;\n\n // cout << node << \" \" << count << endl;\n int neighbour = edges[node];\n if(neighbou... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n const int n = edges.size();\n vector<int> dist(n, -1);\n int ans = -1;\n\n for (int i = 0;i < n;i++) {\n if (dist[i] == -1) {\n ans = max(ans, longest(edges, dist, i, 0));\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size();\n vector<int> indegree(n, 0);\n for(int i = 0; i < n; i++)\n {\n if (edges[i] != -1)\n indegree[edges[i]]++;\n }\n\n queue<int> q;\n for(i... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\npublic:\n bool isCycleDetected = false;\n int cycleDetectedAt = -1;\n\n int dfs(int u, vector<int>& edges, vector<int>& status){\n int v = edges[u];\n if(v == -1){\n status[u] = 2;\n return 0;\n }\n status[u] = 1;\n int lor... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\npublic:\n vector<int> count;\n int maxLen;\n int dfs(int src,int c,vector<int>& edges) {\n if(count[src]) {return count[src];}\n count[src] = c;\n if(edges[src] != -1) {\n maxLen = max(maxLen, 1 + c - dfs(edges[src], c+1, edges));\n }\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\npublic:\n \n int mx; \n \n void dfs(int node,vector<int>& edges,vector<int>& distance_from_start_node,vector<int>& visited,int current_len){\n \n if(visited[node] == 0){\n visited[node] = 1;\n distance_from_start_node[node] = ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\npublic:\n \n int result = -1;\n void dfs(int u ,vector<int>& edges,vector<int> & vis , vector<int>&count ){\n if( u != -1){\n vis[u] =1 ;\n int v = edges[u];\n if(v != -1 && vis[v]==0){\n count[v] = count[u]+1;\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\npublic:\n vector<int> vis, nexts;\n int dfs(int node){\n if(vis[node]) return 0;\n vis[node] = 1;\n return 1 + dfs(nexts[node]);\n }\n\n int longestCycle(vector<int>& edges) {\n int n = edges.size();\n nexts = edges;\n vector<int> inDe... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\npublic:\n void dfs(int node,vector<int>& edges, vector<int>& vis, vector<int>& pathvis, int &ans, int val){\n if(vis[node]==0){\n pathvis[node]=val;\n val++;\n vis[node]=1;\n if(edges[node]!=-1){\n dfs(edges[node], edges, vis, pathvis, ans, val)... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 0 | {
"code": "class Solution {\npublic:\n int ans=-1;\n void dfs(int node, int num, vector<int>& numbered, vector<int>& edges, vector<int>& vis){\n vis[node]=1;\n numbered[node]=num;\n int adj=edges[node];\n\n if(adj!=-1 && numbered[adj]!=-1){\n ans = num-numbered[adj]+1;\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n // int cycle(vector<int>& edges,vector<int> &vis,vector<int> &length, int node,int parent){\n // vis[node]=1;\n // if(parent!=-1)length[node]=length[parent]+1;\n // int newnode=edges[node];\n // int newparent=node;\n // if (newnode==-1)retur... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size(), longest = -1;\n vector<int> visited(n, -1);\n for (int i = 0; i < n; ++i) {\n if (visited[i] != -1) continue; \n int curr = i, time = 0;\n vector<int> path;\n \n wh... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int sz = edges.size();\n vector<int> state(sz, 0); // 0: unvisited, 1: visiting, 2: visited\n vector<int> discoveryTime(sz, -1); // Track discovery time of each node\n int res = -1;\n int time = 0;\n\... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n int dfs(int ind,vector<int>& edges,vector<int>& vis,int visV){\n if((vis[ind]!=0 && vis[ind]!=visV)||edges[ind]==-1)\n return INT_MIN;\n if(vis[ind]==visV){\n int cnt=1,val=edges[ind];\n while(val!=ind){\n cnt++;\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n int dfs(int ind,vector<int>& edges,vector<int>& vis,int visV){\n if((vis[ind]!=0 && vis[ind]!=visV)||edges[ind]==-1)\n return INT_MIN;\n if(vis[ind]==visV){\n int cnt=1;\n int val=edges[ind];\n while(val!=ind){\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n int out = -1 ;\n void DFS(vector<int>& edges , vector<int>&val , vector<bool>&visted, int i , int count){\n if( val[i] ){\n out = max(out , count - val[i]) ;\n return ;\n }\n else if( !visted[i] ){\n val[i] = count++ ;... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n int out = -1 ;\n void DFS(vector<int>& edges , vector<int>&val , vector<bool>&visted, int i , int count){\n if( val[i] ){\n out = max(out , count - val[i]) ;\n return ;\n }\n else if( !visted[i] ){\n val[i] = count++ ;... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic: \n void dfs(int node,vector<int>&vis,vector<int>&edges,stack<int>&st)\n {\n vis[node]=1;\n int adjnode=edges[node];\n if (adjnode==-1) return ;\n if (!vis[adjnode]) {\n dfs(adjnode,vis,edges,st);\n }\n st.push(node);\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n \n // dfs function for finding the length of cycle\n \n void dfs(vector<int>& edges, int u, vector<bool>& vis, int &count)\n {\n vis[u] = true;\n \n count++;\n \n if(edges[u] != -1 && vis[edges[u]] == false)\n {\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n int dfs(int node, int count, vector<int> &edges, vector<bool> &visited, vector<int> &stack) {\n visited[node] = true;\n stack[node] = count;\n int answer = -1;\n int next = edges[node];\n if(next != -1) {\n if(stack[next] != -1) {... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n int ans; \n\n void dfs(int u, vector<int>& edges, vector<bool>& vis, vector<int>& curr_rec, int lvl) \n { \n vis[u] = true;\n curr_rec[u] = lvl;\n\n int v = edges[u];\n\n if(v != -1) \n {\n if(!vis[v]) \n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\nprivate:\n vector<int> path;\n\n int cycleLength(int node,\n int len,\n int comp,\n vector<int>& adjlist,\n vector<int>& visited) {\n\n //already visited node - can be a cycle or part of previous DFS.\n if (visited[node]) {\n if(visited[node]... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\nprivate:\n vector<int> path;\n\n int cycleLength(int node,\n int len,\n int comp,\n vector<int>& adjlist,\n vector<int>& visited) {\n\n //already visited node - can be a cycle or part of previous DFS.\n if (visited[node]) {\n if(visited[node]... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n int result = -1; // To store the length of the longest cycle\n\n void dfs(int u, vector<int>& edges, vector<int>& visited, vector<int>& count, vector<bool>& inRecursion) {\n if (u == -1 || visited[u]) {\n return;\n }\n\n visited[u] = true; ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n void dfs(int node, vector<int>& edges, vector<bool>& visited, vector<int>& stackDepth, int currentDepth, int& maxCycleLength) {\n visited[node] = true;\n stackDepth[node] = currentDepth;\n \n int neighbor = edges[node];\n if (neighbor != -1)... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\nprivate:\n int cycleLength(int node, int len, int comp, vector<int>& adjlist, vector<int>&visited, vector<int> &path) {\n\n //already visited node - can be a cycle or part of previous DFS.\n if (visited[node]) {\n if(visited[node]==comp) { // cycle complete! ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n void dfs(int node, int counter, vector<int>& vis, vector<bool>& dfsvis, int &maxLen, vector<int>& edges) {\n vis[node] = counter;\n dfsvis[node] = true;\n int neigh = edges[node];\n \n if (neigh != -1) {\n if (vis[neigh] == -1) {\... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size();\n vector<int>adj[n];\n vector<int>ind(n,0);\n for(int i=0;i<n;i++)\n {\n if(edges[i]!=-1)\n {\n adj[i].push_back(edges[i]);\n ind[edge... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n int mLength = -1;\n void Dfs(vector<int> &edges, int u, vector<bool> &visited, vector<bool> &inRec){\n visited[u] = true;\n inRec[u] = true;\n int v = edges[u];\n if(v != -1){\n if(!visited[v]){\n Dfs(edges,v, visited, ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n int ans=-1;\n int vis[100005];\n vector<int>v[100005];\n int m[100005];\n void solve(int dep,int x){\n m[x]=dep;\n vis[x]=1;\n for(auto it:v[x])\n {\n if(vis[it]==0){\n solve(dep+1,it);\n }\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n\nint ans=- 1;\n\nvoid dfs(int i,vector<int>& edges,vector<bool>&rec,vector<bool>&visited,vector<int>&count)\n{\n\nif(i!=-1)\n {\n visited[i]=true;\n rec[i]=true;\n\n\n int neigh=edges[i];\n if(neigh!=-1)\n {\n if(!visited[neigh])\n {\n c... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic: \n int result=-1;\n void dfs(vector<int> &edges,vector<bool> &visited,vector<bool> &inRecursion,vector<int>&count,int u){\n\n if(u!=-1){\n visited[u]=true;\n inRecursion[u]=true;\n\n int v=edges[u];\n if(v!=-1 && !visited[v]... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 1 | {
"code": "class Solution {\npublic:\n int result=-1;\n void dfs(int u,vector<int>& edges,vector<bool>& vis,vector<int>& dist,vector<bool>& inRecur){\n if(u!=-1){\n vis[u]=true;\n inRecur[u]=true;\n int v=edges[u];\n if(v!=-1&&vis[v]==0){\n dist[... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\nprivate:\n vector<int> path = vector<int>(1e5, 0);\n int pathlen=0;\n\n int cycleLength(int node,\n int len,\n int comp,\n vector<int>& adjlist,\n vector<int>& visited) {\n\n //already visited node - can be a cycle or part of previous DFS.\n if (visi... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int node,vector<int> adj[],vector<int>& vis,int &cNode)\n {\n vis[node] = 2;\n\n for(auto nb : adj[node])\n {\n if(vis[nb] == 2)\n {\n cNode = nb;\n return;\n }\n else i... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\n int result = -1;\n\n void dfs(int u, vector<int>&edges, vector<bool>&vis, vector<int>&count, vector<bool>&inRecursion)\n {\n if (u!=-1){\n vis[u] = 1;\n inRecursion[u] = 1;\n int v = edges[u];\n if (v!=-1 && !vis[v])\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\n int result = -1;\n\n void dfs(int u, vector<int>&edges, vector<bool>&vis, vector<int>&count, vector<bool>&inRecursion)\n {\n if (u!=-1){\n vis[u] = 1;\n inRecursion[u] = 1;\n int v = edges[u];\n if (v!=-1 && !vis[v])\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\npublic:\nvoid dfs(int node, vector<int> adj[], vector<int> &vis, int &count){\n count+=1;\n vis[node]=1;\n for(auto val : adj[node]){\n if(!vis[val]) dfs(val, adj, vis, count);\n }\n}\n int longestCycle(vector<int>& e) {\n int n = e.size();\n vector<int... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "#define pb push_back\nclass Solution {\npublic:\n void dfs(int node,int &c,vector<int>&vis,vector<int>adj[]){\n vis[node]=1;\n c++;\n for(auto adjnode:adj[node]){\n if(!vis[adjnode]){\n dfs(adjnode,c,vis,adj);\n }\n }\n }\n int longe... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\n map<int,int> mp;\npublic:\n void dfs(int node, vector<int>adj[], vector<int>&vis,\n vector<int>&pathVis,int cnt, int &ans) {\n vis[node] = 1;\n pathVis[node] = 1;\n // mp[node] = cnt;\n for(auto it : adj[node]) {\n if(!vis[it]) {\n... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\n int max(int a, int b)\n {\n return (a > b) ? a : b;\n }\n void dfs(int node, vector<int>& edges, vector<bool> &vis, vector<int>& dis, int &ans)\n {\n if(node == -1) return;\n if(vis[node])\n {\n int count = -1;\n for(int i ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\npublic:\nvoid func(int i,vector<int>adj[],vector<int>&vis,vector<int>&vis1,int t,int &c){\n vis[i]=t;\n vis1[i]=1;\n for(auto it:adj[i]){\n if(vis[it]==1e9){\n func(it,adj,vis,vis1,t+1,c);\n }\n else if(vis1[it]){\n c=max(c,vis[i]-vis[it... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int node, int level, vector<int> adj[], vector<int> &vis, vector<int> &pathvis, int &maxi){\n vis[node] = 1;\n pathvis[node] = level;\n for(auto it : adj[node]){\n if(!vis[it]){\n dfs(it, level+1, adj, vis, pathvis, maxi... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\npublic:\n int dfs(int i, vector<int>&vis, vector<int>adj[], int& cycleLength) {\n vis[i] = 1;\n cycleLength++;\n for (auto it : adj[i]) {\n if (vis[it] == 0) {\n dfs(it, vis, adj, cycleLength);\n }\n }\n return cyc... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size();\n vector<vector<int>> adj(n);\n vector<int> indegree(n, 0);\n\n for (int i = 0; i < n; ++i) {\n if (edges[i] != -1) {\n adj[i].push_back(edges[i]);\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\npublic:\nint ans = -1;\n\n void dfs(int i,vector<int>&vis,vector<int>&v,vector<int>& edges){\n if(i==-1)return ;\n if(vis[i]){\n cout<<\"vis \"<<i<<endl;\n for(auto i:v)cout<<i<<\" \";\n cout<<endl;\n int ind=-1 ;\n f... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\npublic:\nvoid dfs(vector<int>&edges,vector<bool>&visited,vector<int>&count,vector<bool>&included,int i,int count1,int& ans)\n{\n // visited[i]=false;\n if(edges[i]!=-1&&included[edges[i]]==true)\n return;\n if(edges[i]==-1)\n\n {\n visited[i]=false;\n included... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges)\n {\n int n = edges.size();\n\n vector<int> visited(n);\n\n vector<vector<int>> adj(n+1);\n vector<int> indegree(n);\n for(int i=0;i<n;i++)\n {\n int u = i;\n int v = edges... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "#include <bits/stdc++.h>\nclass Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size();\n vector<int>inDegree(n,0);\n vector<vector<int>>adj(n);\n for (int i = 0; i < n; ++i) {\n if (edges[i] != -1) {\n adj[i].push_back(e... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\nprivate:\n int bfs(int i, vector<int>& vis, vector<vector<int>>& adj){\n vis[i] = 1;\n queue<int> q;\n q.push(i);\n\n int cnt = 0;\n while(!q.empty()){\n int node = q.front();\n q.pop();\n for(auto it : adj[node]){\n ... |
2,439 | <p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicat... | 2 | {
"code": "class Solution {\n private:\n bool dfs(int node , vector<vector<int>> & adj,vector<int>& vis){\n vis[node]=2;\n for(auto it : adj[node]){\n if(!vis[it]){\n if(dfs(it,adj,vis)) return true;\n }\n else if(vis[it] == 2) return true;\n }\n ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.