id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
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 ans = -1;\n // topological sort\n vector<int> indegree(edges.size()); \n unordered_set<int> visit;\n\n // calculate in-degree\n for (auto e : edges) {\n if (e != -1) indegree[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 {\npublic:\n int cycle =-1;\n void dfs(int node,int val,vector<vector<int>>&g, vector<int>&vis,int n) {\n if (vis[node]==-1) return ;\n if (vis[node]>0) {\n cycle = max(cycle,val-vis[node]);\n return ;\n }\n vis[node] = val;\n fo... |
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(vector<int> &edges,int node,int &ans,vector<int> &vis){\n int t=0;\n map<int,int> m;\n while(node!=-1&&!vis[node]&&m.count(node)==0){\n m[node]=t;\n vis[node]=1;\n node=edges[node];\n t++;\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... | 2 | {
"code": "class Solution {\npublic:\n int count(int node, vector<vector<int>>& graph, vector<int>& vis){\n queue<int> que;\n vis[node] = 1;\n int count = 1;\n que.push(node);\n\n while(!que.empty()){\n int curr = que.front();\n que.pop();\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... | 2 | {
"code": "class Solution {\npublic:\nint maxi=INT_MIN;\nvector<int>vis;\nvector<vector<int>>adj;\nvector<int>dist;\n void dfs(int node){\n vis[node]=1;\n\n for(auto it:adj[node]){\n if(!vis[it]){\n dist[it]=1+dist[node];\n dfs(it);\n }\n els... |
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\n int dfs(int node, vector<vector<int>>& grid, vector<int>& vis){\n if(!vis[node]){\n vis[node]=1;\n int cnt=1;\n for(auto it:grid[node]){\n cnt+=dfs(it, grid, vis);\n }\n return cnt;\n }\n retu... |
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 vector<int> m;\n vector<vector<int>> adj;\n vector<int> dp;\n int n;\n\n int dfs(int x, int k) {\n if (dp[x] != -1)\n return dp[x];\n if (m[x] != -1)\n return k - m[x];\n\n m[x] = k;\n int ans = 0;\n for (au... |
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 vector<int> m;\n vector<vector<int>> adj;\n vector<int> dp;\n int n;\n\n int dfs(int x, int k) {\n if (dp[x] != -1)\n return dp[x];\n if (m[x] != -1)\n return k - m[x];\n\n m[x] = k;\n int ans = 0;\n for (au... |
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 ans = -1;\n void dfs(vector<int>& edges, unordered_map<int, pair<int, int>>& vis, int node, int key) {\n int dis = 0;\n while(node > -1 && !vis.count(node)) {\n vis[node] = {key, dis++};\n node = edges[node];\n }\n if(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 {\nprivate:\n int n;\n vector<int> edge;\n vector<bool> gvisited;\n vector<bool> visited;\n int ans = 0;\n int maxval = -1;\n\npublic:\n pair<bool,bool> dfs(int start, int ostart) {\n if(gvisited[start]){\n visited[start]=true;\n return {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... | 2 | {
"code": "class Solution {\npublic:\n\n void remove1(int i , set<int> &st , vector<int> &ind ,vector<int> &edges){\n if(st.find(i) != st.end()){\n if(ind[i] == 0){\n st.erase(i);\n if(edges[i] != -1){\n ind[edges[i]]--;\n remove... |
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 c = 0;\n bool flag = false;\n int mx = 0;\n\n int longestCycle(vector<int>& edges) {\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(ed... |
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 ans = INT_MIN;\n void solve(int node,vector<int> adj[],vector<int> &store,vector<int> &vis){\n if(vis[node]){\n int count = -1;\n for(int i = 0;i < store.size();i++){\n if(store[i] == node){\n count = 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:\n int ans = INT_MIN;\n void solve(int node,vector<int> adj[],vector<int> &store,vector<int> &vis){\n if(vis[node]){\n int count = -1;\n for(int i = 0;i < store.size();i++){\n if(store[i] == node){\n count = 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... | 3 | {
"code": "class Solution {\n //TC : DFS : O(N+E) n number of nodes and E no of edges\n //SC : O(3N)\npublic:\n int ans = -1;\n\n void DFS(vector<int>& edges,vector<int>&visited,vector<int>&pathVisited,vector<int>&dist, int node, int distance,vector<vector<int>>&adj)\n {\n visited[node] = 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... | 3 | {
"code": "class Solution {\npublic:\n \n int maxLength = -1;\n unordered_map<int, int> dist;\n void dfs(vector<int> &edges,int cur,vector<bool>& visit, int d){\n if(cur == -1)return ;\n if(visit[cur]){\n if (dist.contains(cur)) {\n maxLength = max(maxLength, d - di... |
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... | 3 | {
"code": "class Solution {\npublic:\n bool isRecursion;\n int ans;\n void dfs(int node,vector<int>&vis,vector<bool>&isRecursion,vector<int>&counts,vector<int>adj[]){\n vis[node]=1;\n isRecursion[node]=true;\n\n for(auto it:adj[node]){\n if(!vis[it]){\n counts[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... | 3 | {
"code": "using ll = long long ;\nusing vl = vector<ll> ;\nusing vvl = vector<vl> ; \n\nclass Solution {\npublic: \n\n ll active_node = 1;\n ll expired_node = 2; \n \n void dfs(ll cur,ll &ans,ll depth, vl &vis,vl& dp, vvl &g){\n\n if(vis[cur]==active_node){\n ans = max(ans,depth-dp[cur]... |
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... | 3 | {
"code": "#include <algorithm>\nusing namespace std;\nclass Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int m = -1;\n set<int> visited;\n\n for(int i = 0; i < edges.size(); i++){\n int current = i;\n int start = i;\n vector<int> path;\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... | 3 | {
"code": "class Solution {\npublic:\n\n int ans=-1;\n\n void dfs(vector<int> adj[],int s,int n,vector<bool> &visited,int l,vector<int> &depth,vector<bool> &recstack)\n {\n visited[s]=true;\n recstack[s]=true;\n depth[s]=l;\n for(auto x:adj[s])\n {\n if(visited[x... |
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... | 3 | {
"code": "class Solution {\npublic:\n bool dfs(vector<int>graph[], int sv, vector<bool>&vis, vector<bool>&recStack, vector<int>&dist, int distance, int &ans){\n vis[sv]=true;\n recStack[sv]=true;\n dist[sv]=distance;\n\n for(int i:graph[sv]){\n if(!vis[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... | 3 | {
"code": "class Solution {\npublic:\n stack<int> st; int n, cnt = 0;\n int max_cycle = 0;\n vector<int> adj[100001]; \n vector<int> t_adj[100001];// transpose graph \n int visited[100001];\n\n // Sắp xếp topo\n void DFS1(int u){\n visited[u] = true;\n for(int v : adj[u]){\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... | 3 | {
"code": "class Solution {\npublic:\n stack<int> st; int n, cnt = 0;\n int max_cycle = 0;\n vector<int> adj[100001]; \n vector<int> t_adj[100001];// transpose graph \n int visited[100001];\n\n // Sắp xếp topo\n void DFS1(int u){\n visited[u] = true;\n for(int v : adj[u]){\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... | 3 | {
"code": "class Solution {\npublic:\n // void f(int i, unordered_map<int,vector<int>>&mpp, vector<bool>&v1, int &cnt){\n // int total=1;\n // v1[i]=1;\n // for(auto vec:mpp[i]){\n // if(!v1[vec]){\n // f(vec,mpp,v1,cnt);\n // total+=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... | 3 | {
"code": "class Solution {\npublic:\n int res=-1;\n void f(int node, unordered_map<int,int>&mpp, vector<bool>&vis, vector<bool>&st, vector<int>&dis){\n vis[node]=1;\n st[node]=1;\n\n if(mpp.find(node)!=mpp.end()){\n int adjnode=mpp[node];\n if(!vis[adjnode]){\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... | 3 | {
"code": "class Solution {\npublic:\n void dfs(int node,vector<int> &vis,vector<int> adj[],stack<int> &st){\n vis[node]=1;\n for(auto it:adj[node]){\n if(!vis[it])\n dfs(it,vis,adj,st);\n }\n st.push(node);\n }\n void dfs3(int node,vector<int> &vis,vecto... |
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... | 3 | {
"code": "class Solution {\npublic:\n bool dfs(int i,vector<int>&cnt,vector<int>&visited,vector<int>&path,vector<int>adj[])\n {\n visited[i]=1;\n path[i]=1;\n cnt.push_back(i);\n for(auto it:adj[i])\n {\n if(!visited[it])\n {\n if(dfs(it,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... | 3 | {
"code": "class Solution {\npublic:\n bool dfs(int node,vector<vector<int>>& adj,vector<bool>& vis,vector<int>& parent,int start,int &len){\n \n vis[node] = 1;\n for(auto nbr : adj[node]) { \n if(!vis[nbr]) { \n parent[nbr] = node;\n bool foundcycle = ... |
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... | 3 | {
"code": "class Solution {\npublic:\n bool isCyclicUtil(vector<vector<int>>& adj, int u, vector<bool>& visited,\n vector<bool>& recStack) {\n\n if (!visited[u]) {\n visited[u] = true;\n recStack[u] = true;\n for (int x : adj[u]) {\n if (!... |
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... | 3 | {
"code": "class Solution {\npublic:\n void topo(int sv,vector<bool>& visited,stack<int>& s,vector<int> adj[]){\n visited[sv]=1;\n for(auto it:adj[sv]){\n if(!visited[it]){\n topo(it,visited,s,adj);\n }\n }\n s.push(sv);\n }\n\n void dfs(int sv... |
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... | 3 | {
"code": "class Solution {\npublic:\n void dfs1(int node, vector<int> adj[], stack<int>& st, vector<int>& vis) {\n vis[node] = 1;\n for (auto it : adj[node]) {\n if (!vis[it]) {\n dfs1(it, adj, st, vis);\n }\n }\n st.push(node);\n }\n\n void d... |
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... | 3 | {
"code": "class Solution {\npublic:\n void dfs1(int node, vector<int> adj[], stack<int>& st, vector<int>& vis) {\n vis[node] = 1;\n for (auto it : adj[node]) {\n if (!vis[it]) {\n dfs1(it, adj, st, vis);\n }\n }\n st.push(node);\n }\n\n void d... |
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... | 3 | {
"code": "#define pb push_back\nclass Solution {\n int n;\npublic:\n\n vector<vector<int>> adj;\n vector<int> vis;\n vector<int> ord;\n Solution(){\n ios_base::sync_with_stdio(0);\n cin.tie(0); cout.tie(0);\n }\n void dfs(int u){\n vis[u] = 1;\n ord.pb(u);\n fo... |
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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size();\n int ans = -1;\n vector<bool> vis(n, false);\n\n for(int i = 0;i < n;i++){\n if(vis[i]) continue;\n vis[i] = true;\n unordered_map<int,int> dfs_vis;\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... | 3 | {
"code": "class Solution {\npublic:\n vector<int>vis;\n stack<int>st;\n vector<int>instack;\n vector<int>dis;\n vector<int>low;\n int timer;\n int ans= INT_MIN;\n void dfs(int node,vector<int>adj[]){\n vis[node]=1;\n dis[node]= low[node]= timer;\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... | 3 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<int>> &g,vector<bool> &vis,int s, int &res, vector<int > &mem)\n {\n if(s == -1)return ;\n \n \n \n if(vis[s])\n {\n int cnt = -1;\n for(int i=0;i<mem.size();i++)\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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size();\n vector<int> visited(n, -1);\n int longest = -1;\n for(int i = 0; i < n; i++){\n if(visited[i] != -1) continue;\n int step = 0;\n int node = 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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size();\n vector<int> visited(n, -1); // -1 means unvisited, otherwise stores the step it was visited\n int ans = -1;\n \n for (int i = 0; i < n; ++i) {\n if (visited[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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n vector<vector<int>> adj(edges.size(),vector<int>());\n vector<bool> visited(edges.size(),false);\n unordered_map<int,int> inDegree;\n for(int i = 0; i < edges.size(); i++){\n inDegree[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... | 3 | {
"code": "class Solution {\npublic:\n \n/*\n Pseudo intuition:\n * THERE WILL BE AT MOST 1 CYCLE FOR EACH CONNECTED COMPONENT\n * BECUASE OF THE CONDITION : \n each node has at most one outgoing edge. \n think of reversing the edges : after reversing each node wi... |
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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size();\n vector<int> visited(n, 0); // To keep track of visited nodes\n vector<int> depth(n, 0); // To store the depth at which each node is visited\n int maxLen = -1;\n\n for (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... | 3 | {
"code": "class Solution {\npublic:\n vector<bool> on_path, vis;\n vector<vector<int>> graph;\n int maxi = 0;\n int count;\n\n bool dfs(int node) {\n vis[node] = on_path[node] = true;\n for (auto next : graph[node]) {\n if (on_path[next]) {\n count++;\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... | 3 | {
"code": "class Solution {\npublic: // kosaraju algorithm find the maximum length of all\n // of the strongly connected component\n int longestCycle(vector<int>& edges) {\n int V = edges.size();\n vector<vector<int>> adj(V);\n \n // Build the adjacency list\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... | 3 | {
"code": "class Solution {\npublic:\n static bool DFS(vector<vector<int>> &adj , vector<bool> &vis , vector<bool> &rec ,int o,int &count){\n vis[o] = true;rec[o] = true;\n\n for(auto it : adj[o]){\n if(vis[it] == false ){\n if (DFS(adj , vis , rec, it,count)){\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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size();\n vector<int> vis(n, 0);\n int maxi = INT_MIN;\n for(int i = 0; i<n; i++){\n // vector<int> pathVis(n, 0);\n int count = 1;\n unordered_map<int, int> mp... |
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... | 3 | {
"code": "class Solution {\npublic:\nint maxi = -1;\nvoid dfs(int src, vector<int>&visited, vector<int>& edges, vector<int> &cnt,\n vector<int>&path)\n{\n visited[src] = 1;\n path[src] = 1;\n\n int nbr = edges[src];\n if(nbr == -1) \n {\n path[src] = 0;\n return;\n }\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... | 3 | {
"code": "class Solution {\npublic:\n vector<int> time;\n vector<bool> visited;\n vector<bool> incycle;\n vector<int> adj[100001];\n int maxanswer=-1;\n int tt=1;\n void dfs(int node){\n visited[node]=true;\n time[node]=tt;\n tt++;\n int z=tt;\n incycle[node]=t... |
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... | 3 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<int>> &adj,vector<bool> &vis,int &cnt,int sv){\n vis[sv]=true;\n cnt++;\n for(auto it:adj[sv]){\n if(!vis[it]){\n dfs(adj,vis,cnt,it);\n }\n }\n }\n\n int longestCycle(vector<int>& 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... | 3 | {
"code": "class Solution {\npublic:\n bool dfs(int ind,vector<int> adjlist[],vector<int>&vis,vector<int>&pathvis){\n vis[ind]=1;\n pathvis[ind]=1;\n for(auto &it:adjlist[ind]){\n if(vis[it]==-1){\n if(dfs(it,adjlist,vis,pathvis)) return true;\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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n // Loop through all nodes in the graph\n int maxm = 0;\n int n = edges.size();\n vector<int> visited(n, false);\n for (int i = 0; i < edges.size(); i++) {\n// cout << \"Cycle length startin... |
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... | 3 | {
"code": "class Solution {\npublic:\n \n int lengthdfs(int s,vector<int> &vis,vector<vector<int>> &g){\n if(vis[s])return 0;\n vis[s]=1;\n\n int ans=0;\n for(auto ch : g[s]){\n ans+=lengthdfs(ch,vis,g);\n }\n return ans+1;\n }\n int longestCycle(vector... |
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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n set<int> possible;\n int n=edges.size();\n vector<vector<int>> adj(n);\n for(int i=0;i<edges.size();i++){\n if(edges[i]==-1) continue;\n adj[i].push_back(edges[i]);\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... | 3 | {
"code": "class Solution {\npublic:\n int dfs(vector<vector<int>>&adj, int u, vector<bool>&vis){\n if (vis[u]) return 0;\n vis[u] = true;\n int res = 0;\n for (auto &v : adj[u]){\n if (!vis[v]){\n int prob = dfs(adj, v, vis);\n res = max(res, pr... |
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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size(), res = -1;\n vector<bool> v(n, false); // has cycle\n for (int start = 0; start < n; ++start) {\n if (v[start]) continue;\n int cur = start, l = 1;\n unordered_... |
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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n = edges.size(), res = -1;\n vector<bool> v(n, false); // has cycle\n for (int start = 0; start < n; ++start) {\n if (v[start]) continue;\n int cur = start, l = 1;\n unordered_... |
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... | 3 | {
"code": "class Solution {\npublic:\n int answer = -1;\n void dfs(int node, vector<int>& edges, unordered_map<int, int>& dist, vector<bool>& visit) {\n visit[node] = true;\n int neighbor = edges[node];\n\n if (neighbor != -1 && !visit[neighbor]) {\n dist[neighbor] = dist[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... | 3 | {
"code": "class Solution {\npublic:\n int ans = -1; // Initialize ans to -1 to store the length of the longest cycle.\n \n // DFS function to traverse the graph and detect cycles\n void dfs(int node, vector<int>& edges, vector<bool>& visited, unordered_map<int, int>& dist) \n {\n 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... | 3 | {
"code": "class Solution {\npublic:\n int ans;\n void dfs(vector<int>& edges, int curr, vector<int>& vis, unordered_set<int>& nodes, int pos){\n\n if(curr == -1){\n return;\n }\n\n if(vis[curr] != -1){\n\n // we have seen this node before\n if(nodes.find(cu... |
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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n int n=edges.size();\n vector<int> vis(n);\n int ans=-1;\n function<void(int, unordered_map<int,int>&)> dfs=[&](int node, unordered_map<int,int> &dist)->void{\n vis[node]=1;\n int node1=... |
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... | 3 | {
"code": "class Solution {\nint n;\nint ans;\nvector<vector<int>> gr;\npublic:\n void init(int nodes){\n ans =-1;\n n = nodes;\n gr.resize(n+1);\n return;\n }\npublic: \n void dfs(int node, vector<bool>& vis, vector<bool>& path, stack<int>& s){\n\n vis[node] = 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... | 3 | {
"code": "class Solution {\npublic:\n void detectCycles(int src, int pos, vector<int>edges[], vector<bool>&visited, map<int, int>&mp, int &ans) {\n\n visited[src] = true;\n mp[src] = pos;\n // cout<<\"src=\"<<src<<endl;\n for(int node : edges[src]) {\n \n if(!... |
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... | 3 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<int>>&adj,int node,stack<int>&st,vector<int>&visited){\n visited[node ] = 1;\n for(auto i:adj[node]){\n if(!visited[i]){\n dfs(adj,i,st,visited);\n }\n }\n st.push(node);\n }\n 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... | 3 | {
"code": "void dfs(int i, vector<vector<int>>& adjlist, vector<int>& visited, stack<int>& st)\n{\n visited[i] = 1;\n for(auto it: adjlist[i])\n {\n if(!visited[it])\n {\n dfs(it,adjlist,visited,st);\n }\n }\n st.push(i);\n}\n\nvoid newdfs(int i, vector<vector<int>>& rev... |
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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n \n int n = edges.size(), mx = 0;\n vector<vector<int>> graph(n), reverse_graph(n);\n \n for(int i = 0; i < n; i++)\n if(edges[i] >= 0)\n graph[i].push_back(edges[i]), reverse... |
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... | 3 | {
"code": "void topo(int node,vector<bool> &vis,stack<int>& st,vector<vector<int>>& adj){\n vis[node] = true;\n \n for(auto c:adj[node]){\n if(!vis[c]){\n topo(c,vis,st,adj);\n }\n }\n \n st.push(node);\n\t \n}\n\t\nvoid dfs(int node,vector<bool> &vis,int &count_nodes,vec... |
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... | 3 | {
"code": "class Solution {\npublic:\n int solve(vector<int>& edges , int src , vector<bool>& parent , int o , map<int, int>& mp){\n\n if(src == -1){\n return -1;\n }\n \n if(parent[src] && src == o){\n return 0;\n }\n if(parent[src]){\n re... |
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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n \n //this solution is faster than mine, why?\n //https://leetcode.com/problems/longest-cycle-in-a-graph/discuss/2357750/Simple-DFS-Traversal-or-Easy-Explanation-or-C%2B%2B\n int len=-1;\n vector<bool>... |
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... | 3 | {
"code": "class Solution {\npublic:\n int longestCycle(vector<int>& edges) {\n \n //this solution is faster than mine, why?\n //https://leetcode.com/problems/longest-cycle-in-a-graph/discuss/2357750/Simple-DFS-Traversal-or-Easy-Explanation-or-C%2B%2B\n int len=-1;\n vector<bool>... |
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... | 3 | {
"code": "class Solution {\npublic:\n// void toplogy(int s, vector<int> &vis ,vector<vector<int>> &g, stack<int> &st){\n// vis[s]=1;\n// for(auto child:g[s]){\n// if(!vis[child]){\n// toplogy(child,vis,g,st);\n// }\n// }\n// st.push(s);\n// }\n\n// bool dfs(int s... |
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... | 3 | {
"code": "class Solution {\npublic:\n\n bool check(int node,vector<int> & vis,vector<vector<int>> & adj,set<int> & st){\n\n vis[node]=1;\n st.insert(node); //pathvisited\n for(auto it:adj[node]){\n if(!vis[it]){\n if(check(it,vis,adj,st)){\n return... |
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... | 3 | {
"code": "class Solution {\npublic:\n int max_ = -1;\n void dfs(int node, vector<int> &edges, unordered_map<int,int> &dist, vector<bool> &vis) {\n vis[node] = true;\n int neighbour = edges[node];\n cout<<node<<endl;\n\n if(neighbour!=-1 && vis[neighbour]==false) {\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... | 3 | {
"code": "class Solution {\npublic:\n int ans = -1;\n void dfs(vector<int>& adj,int r, vector<bool>& vis, unordered_map<int,int>& currStack, int count){\n if (r==-1 or vis[r]) return;\n vis[r]=1;\n currStack[r]=count;\n int c = adj[r];\n if(currStack[c]){\n ans = m... |
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... | 3 | {
"code": "class Solution {\npublic:\n void dfs(vector<int>& edges,int src,int &cnt,unordered_map<int, int>&dist,vector<bool>&vis)\n {\n //vis[src]=true;\n int next=edges[src];\n if (next==-1)return;\n if (dist[next]!=0 && vis[next])\n {\n cnt=max(cnt,dist[src]-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... | 3 | {
"code": "class Solution {\npublic:\n int isCycle(int sv, vector<vector<int>>& edges, vector<bool>& vis,\n set<int>& s) {\n vis[sv] = true;\n s.insert(sv);\n for (int i = 0; i < edges[sv].size(); i++) {\n if (s.find(edges[sv][i]) != s.end())\n return s... |
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... | 3 | {
"code": "class Solution {\npublic:\n int isCycle(int sv, vector<vector<int>> &edges, vector<bool> &vis, set<int> &s){\n vis[sv]=true;\n s.insert(sv);\n for(int i=0;i<edges[sv].size();i++){\n if(s.find(edges[sv][i]) != s.end())\n return sv;\n if(!vis[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... | 3 | {
"code": "class Solution {\npublic:\n int isCycle(int sv, vector<vector<int>> &edges, vector<bool> &vis, set<int> &s){\n vis[sv]=true;\n s.insert(sv);\n for(int i=0;i<edges[sv].size();i++){\n if(s.find(edges[sv][i]) != s.end())\n return sv;\n if(!vis[edges... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\nprivate:\n static constexpr int MINV = -100000, MAXV = 100000;\n static constexpr unsigned RANGEV = MAXV - MINV + 1u;\n typedef unsigned long long u64;\n static u64 index[RANGEV];\n\n static unsigned lo(const u64 p) __attribute__((always_inline)) {\n return p & UINT_... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\nprivate:\n static constexpr int MINV = -100000, MAXV = 100000;\n static constexpr unsigned RANGEV = MAXV - MINV + 1u;\n typedef unsigned long long u64;\n static u64 index[RANGEV];\n\n static unsigned lo(const u64 p) __attribute__((always_inline)) {\n return p & UINT_... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\n \npublic:\n vector<int> restoreArray(const vector<vector<int>>& adjacentPairs) {\n unordered_map<int, pair<int,int>> m;\n for (const auto& p : adjacentPairs)\n {\n auto [i0, u0] = m.insert({p[0], {INT_MAX, INT_MAX}});\n if (i0->second.firs... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\n \npublic:\n vector<int> restoreArray(const vector<vector<int>>& adjacentPairs) \n {\n unordered_map<int, pair<int,int>> m;\n for (const auto& p : adjacentPairs)\n {\n auto [i0, u0] = m.insert({p[0], {INT_MAX, INT_MAX}});\n if (i0->secon... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\n \npublic:\n vector<int> restoreArray(const vector<vector<int>>& adjacentPairs) \n {\n unordered_map<int, pair<int,int>> m;\n for (const auto& p : adjacentPairs)\n {\n auto [i0, u0] = m.insert({p[0], {INT_MAX, INT_MAX}});\n if (i0->secon... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n int n = adjacentPairs.size() + 1;\n unordered_map<int, vector<int>> neighbor(n);\n for (auto& adjacentPair : adjacentPairs) {\n neighbor[adjacentPair[0]].push_back(adjacentPair[1]... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n std::unordered_map<int, std::vector<int>> M;\n for (auto &v : adjacentPairs) {\n M[v[0]].push_back(v[1]);\n M[v[1]].push_back(v[0]);\n }\n int s;\n for (a... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n\nvector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n\n // Step 1: Create a map to store each element and its adjacent elements.\n\n unordered_map<int, vector<int>> adj;\n\n // Step 2: Populate the map with pairs.\n\n for (const auto& pair : adjacentPairs... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjP) {\n unordered_map<int,vector<int>> mapping;\n for(auto& x:adjP){\n if(mapping.find(x[0]) != mapping.end()){\n mapping[x[0]][0]++;\n mapping[x[0]].push_back(x[1]);\n ... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n unordered_map<int,vector<int>>v;\n for(int i=0;i<adjacentPairs.size();i++){\n v[adjacentPairs[i][0]].push_back(adjacentPairs[i][1]);\n v[adjacentPairs[i][1]].push_back(adjacen... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n int n = adjacentPairs.size();\n unordered_map<int, vector<int>>gp;\n for(auto& it: adjacentPairs){\n gp[it[0]].push_back(it[1]);\n gp[it[1]].push_back(it[0]);\n ... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n unordered_map<int, vector<int>> graph;\n\n for (auto& edge : adjacentPairs) {\n graph[edge[0]].push_back(edge[1]);\n graph[edge[1]].push_back(edge[0]);\n }\n \n ... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n int start = 0;\n unordered_map<int, vector<int>> next;\n for (auto& vec: adjacentPairs) {\n next[vec[0]].push_back(vec[1]);\n next[vec[1]].push_back(vec[0]);\n }... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n int n = adjacentPairs.size();\n vector<int> ans(n + 1, 0);\n map<int, vector<int>> mp;\n for (auto& pair : adjacentPairs) {\n mp[pair[0]].push_back(pair[1]);\n m... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n\n\n\n // Some difficulties. Even though tracking odd and evens tells us the odd elements are\n // surely the ones on the corners, still need to preserve relative ordering of list construction\n ... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\nstd::vector<int> restoreArray(std::vector<std::vector<int>>& adjacentPairs)\n{\n if (adjacentPairs.size() == 1)\n return { adjacentPairs[0][0], adjacentPairs[0][1] };\n std::unordered_map<int, std::vector<int>> map;\n for (const std::vector<int>& pair : adjacentPa... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n // Construct an adjacency list from the edge list\n\n // Keys: elements in our list\n // Values: adjacent elements to the current elment in original list\n // Note: edges are undirect... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n unordered_map<int,vector<int>> mp;\n\n for(int i=0; i<adjacentPairs.size(); i++){\n mp[adjacentPairs[i][0]].push_back(adjacentPairs[i][1]);\n mp[adjacentPairs[i][1]].push_back... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n map<int,vector<int>>m;\n for(int i=0;i<adjacentPairs.size();i++){\n m[adjacentPairs[i][0]].push_back(adjacentPairs[i][1]);\n m[adjacentPairs[i][1]].push_back(adjacentPairs[i][... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adj) {\n map<int,pair<int,int>> mp;\n for(auto it : adj){\n if(mp.find(it[0]) != mp.end()){\n mp[it[0]].second = it[1];\n }\n else mp[it[0]] = {it[1], INT_MAX};\n ... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n unordered_map<int,vector<int>>adj;\n for(vector<int>& v:adjacentPairs){\n adj[v[0]].push_back(v[1]);\n adj[v[1]].push_back(v[0]);\n }\n queue<pair<int,int>>q;\n ... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) \n {\n int n=adjacentPairs.size();\n vector<int>ans;\n map<int,vector<int>>mpp;\n for(int i=0;i<n;i++)\n {\n mpp[adjacentPairs[i][0]].push_back(adjacentPairs[i][1]);\... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n vector<int>res;\n map<int,vector<int>>m;\n for(int i =0; i < adjacentPairs.size(); i++){\n m[adjacentPairs[i][0]].push_back(adjacentPairs[i][1]);\n m[adjacentPairs[i][1... |
1,866 | <p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>
<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where ... | 0 | {
"code": "class Solution {\npublic:\n vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n int n = adjacentPairs.size();\n map<int,vector<int>> mp;\n int start;\n for(int i = 0;i<n;i++){\n if(mp.find(adjacentPairs[i][0]) == mp.end()){\n mp[adjacent... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.