id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,333
<p>You are given a <strong>0-indexed</strong> integer array <code>mapping</code> which represents the mapping rule of a shuffled decimal system. <code>mapping[i] = j</code> means digit <code>i</code> should be mapped to digit <code>j</code> in this system.</p> <p>The <strong>mapped value</strong> of an integer is the ...
3
{ "code": "class Solution {\npublic:\n vector<int> mp;\n unordered_map<int, int> cache;\n vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {\n mp = mapping;\n sort(nums.begin(), nums.end(), [this](int &a, int &b){ return mapped(a) < mapped(b);});\n\n return nums;\n }\n...
1,333
<p>You are given a <strong>0-indexed</strong> integer array <code>mapping</code> which represents the mapping rule of a shuffled decimal system. <code>mapping[i] = j</code> means digit <code>i</code> should be mapped to digit <code>j</code> in this system.</p> <p>The <strong>mapped value</strong> of an integer is the ...
3
{ "code": "class Solution {\nprivate: \nvector<int>map_nums(vector<int>&mapping, vector<int>nums){\n for (int i = 0; i < nums.size(); ++i) {\n string num = std::to_string(nums[i]);\n\n for (int j = 0; j < num.size(); ++j) {\n int p = num[j] - '0'; \n num[j] = static_cast<char>(...
1,333
<p>You are given a <strong>0-indexed</strong> integer array <code>mapping</code> which represents the mapping rule of a shuffled decimal system. <code>mapping[i] = j</code> means digit <code>i</code> should be mapped to digit <code>j</code> in this system.</p> <p>The <strong>mapped value</strong> of an integer is the ...
3
{ "code": "class Solution {\nprivate: \nvector<int>map_nums(vector<int>&mapping, vector<int>nums){\n for (int i = 0; i < nums.size(); ++i) {\n string num = std::to_string(nums[i]);\n\n for (int j = 0; j < num.size(); ++j) {\n int p = num[j] - '0'; \n num[j] = static_cast<char>(...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
0
{ "code": "class Solution {\npublic:\n \n vector<int>adj[1001];\n \n bool visited[1001];\n \n vector<int>tmp;\n \n void dfs(int a){\n visited[a] = 1;\n \n for(int j=0; j<adj[a].size(); j++){\n if(!visited[adj[a][j]]){\n tmp.push_back(adj[a][j]);\n...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
0
{ "code": "class Solution {\npublic:\n vector<int> adj[1000];\n vector<int> visited;\n int flag[1000];\n\n void dfs(int v)\n {\n flag[v] = 1;\n for (int u : adj[v])\n if (!flag[u])\n {\n visited.push_back(u); \n dfs(u);\n }\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> ancestors(n);\n vector<vector<int>> outgoing(n);\n for (const auto& e : edges) {\n ancestors[e[1]].push_back(e[0]);\n outgoing[e[0]].push_bac...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<int> adj[n];\n vector<int> indegree(n, 0);\n queue <int> q;\n\n for(auto& e : edges){\n adj[e[0]].push_back(e[1]);\n indegree[e[1]]++;\n }\n\n...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> adj(n);\n vector<int> indegree(n,0);\n for (auto& v : edges) {\n adj[v[0]].push_back(v[1]);\n indegree[v[1]]++;\n }\n queue<int...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> adj(n);\n vector<int> indegree(n,0);\n for (auto& v : edges) {\n adj[v[0]].push_back(v[1]);\n indegree[v[1]]++;\n }\n queue<int...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
0
{ "code": "class Solution {\npublic:\nvoid DFS(int ancestor,int node,vector<vector<int>> &ancestors,vector<int>adj[]){\n for(auto it:adj[node]){\n if(ancestors[it].empty() || ancestors[it].back()!=ancestor)\n { ancestors[it].push_back(ancestor);\n DFS(ancestor,it,ancestors,adj);}\n }\n}\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
0
{ "code": "class Solution {\npublic:\n void add(vector<vector<int>>& result, vector<vector<int>>&graph,int n, int i) {\n for (auto nums : graph[n]) {\n if (find(result[i].begin(), result[i].end(), nums) == result[i].end()) {\n result[i].push_back(nums);\n add(result,...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
0
{ "code": "class Solution {\npublic:\nvoid DFS(int ancestor,int node,vector<vector<int>> &ancestors,vector<int>adj[]){\n for(auto it:adj[node]){\n if(ancestors[it].empty() || ancestors[it].back()!=ancestor)\n { ancestors[it].push_back(ancestor);\n DFS(ancestor,it,ancestors,adj);}\n }\n}\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
0
{ "code": "class Solution {\npublic:\nvoid DFS(int ancestor,int node,vector<vector<int>> &ancestors,vector<int>adj[]){\n for(auto it:adj[node]){\n if(ancestors[it].empty() || ancestors[it].back()!=ancestor)\n { ancestors[it].push_back(ancestor);\n DFS(ancestor,it,ancestors,adj);}\n }\n}\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n // Initialize adjacency list for each node and ancestors list\n vector<vector<int>> adjacencyList(n);\n vector<vector<int>> ancestors(n);\n\n // Populate the adjacency list wit...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
0
{ "code": "class Solution {\npublic:\nvoid dfs(int nd, vector<vector<int>> &ans,int pu,vector<vector<int>> &gr){\n for(auto a:gr[nd]){\n int val=-1;\n if(!(ans[a].empty())) val = ans[a].back();\n if(val!=pu){\n ans[a].push_back(pu);\n dfs(a,ans,pu,gr);\n };\n };\n};\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\nauto _=[]()noexcept{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0;}();\nint adj[1000][1000];\nint asz[1000];\nclass Solution {\n int sz;\n vector<int> tmp;\n void update(vector<vector<int>> &r,int ind){\n if(!r[ind].empty()) return;\n if(as...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges){\n vector<vector<int>> adj(n, vector<int>());\n for(auto &e:edges){\n adj[e[1]].push_back(e[0]);\n }\n\n vector<bool> vis(n, false);\n vector<vector<int>> ans(n, ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\n\n void DFS(map<int, vector<int>>& graph, int i, int j, vector<vector<int>>& ans, vector<bool>& visited){\n\n visited[j] = true;\n for(auto node : graph[j]){\n\n if(visited[node] == false){\n\n ans[node].push_back(i);\n DFS(graph, ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\n vector<vector<int>> adjInList;\n vector<vector<bool>> finalOut; //set will increade impedence in covering out back to vector. instead use bitset - since nodes are indexed 0 to n-1\n vector<bool> cached;\n\n void Ancenstors(int start) {\n //start valid\n if (star...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\n void dfs(int node,vector<vector<int>>&adj,vector<int>&vis,vector<int>&ls){\n vis[node]=1;\n \n for(auto it:adj[node]){\n if(!vis[it]) {ls.push_back(it);dfs(it,adj,vis,ls);}\n }\n \n }\npublic:\n vector<vector<int>> getAncestors(int n...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\n void dfs(int node,vector<vector<int>>&adj,vector<int>&vis,vector<int>&ls){\n vis[node]=1;\n \n for(auto it:adj[node]){\n if(!vis[it]) {ls.push_back(it);dfs(it,adj,vis,ls);}\n }\n \n }\npublic:\n vector<vector<int>> getAncestors(int n...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n unordered_map<int,vector<int>> adj;\n void dfs(int u, vector<bool>& visited){\n visited[u] = true;\n for(auto &nbr : adj[u]){\n if(!visited[nbr]) dfs(nbr,visited);\n }\n }\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n unordered_map<int,vector<int>> adj;\n void dfs(int u, vector<bool>& visited){\n visited[u] = true;\n for(auto &nbr : adj[u]){\n if(!visited[nbr]) dfs(nbr,visited);\n }\n }\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\nvoid dfs(int i,vector<bool>&vis,vector<int>&currans, unordered_map<int,list<int>>&adj){\n vis[i]=true;\n for(auto k:adj[i]){\n if(!vis[k]){\n dfs(k,vis,currans,adj);\n }\n \n }\n}\n vector<vector<int>> getAncestors(int n, vector<vector...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>>res(n, vector<int>());\n unordered_map<int, set<int>>mp;\n for(auto it : edges){\n mp[it[0]].insert(it[1]);\n }\n queue<int> q;\n fo...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> ans(n);\n vector<int> adj[n];\n for(auto &it : edges) {\n int u = it[0], v = it[1];\n adj[u].push_back(v);\n }\n\n for(int i = ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<int>adj[n];\n for(int i=0;i< edges.size();i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n }\n vector<vector<int>>ans(n);\n\n vector<bool>vis(n,false...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "#pragma GCC optimize(\"03\")\nstatic int __star = []{\n ios_base::sync_with_stdio(0);\n cin.tie(NULL),cout.tie(NULL);\n return 0;\n}();\nclass Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> adj(n);\n vector<vector<...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<int>adj[n];\n for(auto i: edges){\n adj[i[0]].push_back(i[1]);\n }\n vector<vector<int>>ans(n);\n for(int i =0 ; i<n ; i++){\n queue<int>que...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>>ans(n);\n vector<vector<int>>adj(n);\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n }\n //checking parent 0 to n-1 is parent of whi...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> adj(n);\n for(int i = 0;i<edges.size();i++)\n {\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n vector<vector<int>> ancestors(n);\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\nclass Solution {\npublic:\n vector<int> graph[1001];\n void dfs(int i, stack<int> &s, vector<bool> &vis){\n vis[i] = true;\n for(int chi: graph[i]){\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> parents(n);\n for(const auto edge : edges)\n {\n parents[edge[1]].push_back(edge[0]);\n }\n\n vector<vector<int>> ans(n);\n\n for(i...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\nvector<vector<int>> getAncestors(int n, vector<vector<int>> &edges)\n{\n unordered_map<int, list<int>> adj;\n\n for (int i = 0; i < edges.size(); i++)\n {\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n\n vector<vector<int>> ans(n);\n for (int i = 0; i <...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n void dfs(vector<vector<int>>& g,int s,int r,vector<bool>& vis,vector<int>& ans)\n {\n vis[r]=true;\n for(auto e: g[r])\n {\n if(!vis[e])\n {\n ans.push_back(e);\n dfs(g,s,e,vis,ans);\n }\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>>ans;\n vector<int>adj[n];\n for(auto e:edges){\n int u=e[0];\n int v=e[1];\n adj[v].push_back(u);\n }\n for(int i=0;i...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> g (n);\n for (auto elem : edges) {\n g[elem[1]].push_back(elem[0]);\n }\n\n std::vector<std::vector<int>> anc;\n for (int i = 0; i < n; ++...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<int> filter(vector<int> result){\n sort(result.begin(),result.end()) ;\n vector<int> r ;\n r.push_back(result[0]) ;\n for(int i=1; i<result.size(); i++){\n if(result[i]!=result[i-1]) r.push_back(result[i]) ;\n }\n re...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>>v;\n unordered_map<int,vector<int>>adj;\n for(auto x:edges){\n int child=x[1];\n int parent=x[0];\n adj[child].push_back(parent);\n...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\n vector<vector<int>> parent;\n void merge(int num,int a){\n vector<int> ans;\n int i=0,j=0,prev=-1;\n // cout<<num<<\"start\"<<a<<endl;\n while(i<parent[num].size() || j<parent[a].size()){\n // cout<<parent[num][i]<<...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\n vector<vector<int>> parent;\n void merge(int num,int a){\n vector<int> ans;\n int i=0,j=0,prev=-1;\n while(i<parent[num].size() || j<parent[a].size()){\n if(j>=parent[a].size() || (i<parent[num].size() && parent[num][i]<parent...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> ans(n, vector<int>());\n\n helper(n, edges, ans);\n\n return ans;\n }\n\n void helper(int n, vector<vector<int>>& edges, vector<vector<int>>& ans)\n {\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> ans(n, vector<int>());\n\n helper(n, edges, ans);\n\n return ans;\n }\n\n void helper(int n, vector<vector<int>>& edges, vector<vector<int>>& ans)\n {\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n void dfs(int node,vector<int> &dup,unordered_map<int,list<int> > &adj,unordered_map<int,bool > &visited){\n visited[node] = true;\n for (auto nbr : adj[node]){\n if (!visited[nbr]){\n dup.push_back(nbr);\n dfs(nbr,dup,adj...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n void dfs(int node, vector<int>& vis, vector<vector<int>>& v) {\n vis[node] = 1;\n for (auto it : v[node]) {\n if (vis[it] == 0) { // Change 'vis[node]' to 'vis[it]'\n dfs(it, vis, v);\n }\n }\n}\n\nvector<vector<int>> getAncestors(int n, vect...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> ans(n, vector<int>());\n\n helper(n, edges, ans);\n\n return ans;\n }\n\n void helper(int n, vector<vector<int>>& edges, vector<vector<int>>& ans)\n {\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n void dfs(int node, vector <int> &vis, vector <vector <int>> &adj, vector <int> &cur)\n { \n\n vis[node] = 1;\n for(auto i: adj[node])\n {\n if(!vis[i])\n {\n cur.push_back(i);\n dfs(i, vis, adj, cur...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> ans(n, vector<int>());\n\n helper(n, edges, ans);\n\n return ans;\n }\n\n void helper(int n, vector<vector<int>>& edges, vector<vector<int>>& ans)\n {\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> ans(n, vector<int>());\n\n helper(n, edges, ans);\n\n return ans;\n }\n\n void helper(int n, vector<vector<int>>& edges, vector<vector<int>>& ans)\n {\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) \n {\n vector<vector<int>>mat(n,vector<int>(n,0)),ans;\n unordered_set<int>st;\n for(int i=0;i<n;++i)\n {\n st.insert(i);\n }\n vector<int>g[n],vis(n,0)...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> answer(n, vector<int>()), graph(n, vector<int>());\n vector<int> indegree(n, 0);\n for(int i = 0; i < edges.size(); i++)\n {\n graph[edges[i][0]]...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<int> merge(vector<int>& parent, vector<int>& child){\n\n int n = parent.size(), m = child.size();\n int i = 0, j =0;\n\n vector<int> res;\n while(i < n && j < m){\n\n if(parent[i] < child[j]){\n while(i+1 < n && par...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n vector<int> merge(vector<int>& parent, vector<int>& child){\n\n int n = parent.size(), m = child.size();\n int i = 0, j =0;\n\n vector<int> res;\n while(i < n && j < m){\n\n if(parent[i] < child[j]){\n while(i+1 < n && par...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "#include <vector>\n#include <stack>\n\nusing namespace std;\n\nclass Solution {\npublic:\n void findTopoSort(int node, vector<int>& vis, vector<int> adj[]) {\n vis[node] = 1;\n\n for (auto it: adj[node]) {\n if (!vis[it]) {\n findTopoSort(it, vis, adj);\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\nvoid dfs(int cur,int node,vector<int> adj[],vector<vector<int>> &v,vector<int> &vis){\n vis[node] = 1;\n for(auto it : adj[node]){\n if(!vis[it]){\n v[cur].push_back(it);\n dfs(cur,it,adj,v,vis);\n }\n }\n}\...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n\n void dfs(int node,int parent,vector<int> adj[], vector<vector<int>> &ans, vector<int> &vis){\n vis[node]=1;\n for(auto i:adj[node]){\n if(vis[i]==0){\n ans[i].push_back(parent);\n dfs(i,parent,adj,ans,vis);\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n void dfs(int i,int an,vector<int>adj[],vector<int>&vis,vector<vector<int>>&ans){\n vis[i]=1;\n for(auto it:adj[i]){\n if(!vis[it]){\n ans[it].push_back(an);\n dfs(it,an,adj,vis,ans);\n }\n }\n }\n vector<vector<int>> g...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n void DFS(vector<int>adj[],int ind,vector<int>&vis){\n vis[ind]=1;\n for(auto it:adj[ind]){\n if(!vis[it]){\n DFS(adj,it,vis);\n }\n }\n }\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
1
{ "code": "class Solution {\npublic:\n void dfs(int i, int par, vector<int> adj[], vector<vector<int>>& ans, vector<int>& vis) {\n vis[i] = 1;\n if ( i != par ) {\n ans[i].push_back(par);\n } \n for( int child : adj[i] ) {\n if(vis[child]) continue;\n df...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
2
{ "code": "struct Node {\n unordered_set<int> ancestors;\n vector<int> parents;\n bool coloured = false;\n\n void traverseUp(vector<Node>& graph) {\n if(coloured) return;\n for(auto parent : parents) {\n graph[parent].traverseUp(graph);\n ancestors.insert(parent);\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<unordered_set<int>> ans(n);\n vector<vector<int>> anc(n, vector<int>());\n vector<bool> visited(n, false);\n for (auto & edge: edges) {\n anc[edge[1]].push_ba...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
2
{ "code": "class Solution {\npublic:\n void dfs(int node, vector<int> adj[], vector<int>& vis, vector<int>& temp) {\n vis[node] = 1;\n for (auto it : adj[node]) {\n if (!vis[it]) {\n temp.push_back(it);\n dfs(it, adj, vis, temp);\n }\n }\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
2
{ "code": "class Solution {\npublic:\n\n vector<vector<int>> g;\n\n void dfs(int node, vector<int> &vis, vector<int> &temp){\n vis[node] = 1;\n for(auto &x: g[node]){\n if(!vis[x]){\n temp.push_back(x);\n dfs(x, vis, temp);\n }\n }\n }\...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> ans;\n void dfs(vector<int> adj[], int p, int v, vector<int> &vis){\n vis[v] = 1;\n for(auto it : adj[v]){\n if(!vis[it]){\n ans[it].push_back(p);\n dfs(adj, p, it, vis);\n }\n }\n...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
2
{ "code": "class Solution {\npublic:\n\n void dfs( int i, vector<int> adj[] ,vector<int>& vis, vector<int>& temp ){\n\n if(vis[i]) return;\n vis[i] = 1;\n for( auto node : adj[i]){\n if(vis[node] == 0)\n temp.push_back(node);\n dfs(node, adj, vis, temp);\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) \n {\n vector<vector<int>>adj(n);\n\n queue<int>q;\n vector<int>indegree(n,0);\n\n for(auto it: edges)\n {\n adj[it[0]].push_back(it[1]);\n indegree...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n unordered_map<int, vector<int>> adjList;\n vector<int> inDegree(n, 0);\n\n for(auto edge : edges){\n adjList[edge[0]].push_back(edge[1]);\n inDegree[edge[1]]++;\...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
2
{ "code": "class Solution {\nprivate:\n void dfs(int node, unordered_map<int, vector<int>>& adj, vector<bool>& visited, set<int>& ancestors) {\n // Mark the node as visited\n visited[node] = true;\n\n // Explore the parents (ancestors) of the current node\n for (int parent : adj[node]) {\n if (!...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
2
{ "code": "class Solution {\nprivate:\n void dfs(int node, unordered_map<int, vector<int>>& adj, vector<bool>& visited, set<int>& ancestors) {\n visited[node] = true;\n for (int parent : adj[node]) {\n if (!visited[parent]) {\n dfs(parent, adj, visited, ancestors);\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
2
{ "code": "class Solution {\npublic:\n\n void dfs(int curr,vector<vector<int>> &adjList, vector<unordered_set<int>> &child){\n for(auto x:adjList[curr]){\n if(child[x].size()==0){\n dfs(x,adjList,child);\n }\n child[curr].insert(x);\n for(auto p:chi...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<set<int>> ancs(n, set<int>());\n vector<vector<int>> graph(n, vector<int>());\n vector<int> indegrees(n, 0);\n\n for (auto &e: edges) {\n graph[e[0]].push_bac...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n unordered_map<int,vector<int>> mp; \n vector<vector<int>> ans; \n vector<set<int>> temp(n);\n vector<int> indegree(n, 0); \n\n for (const auto edge : edges) {\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<set<int>> res(n, set<int>());\n map<int, int> indeg;\n map<int, vector<int>> adj;\n for(int i=0; i<edges.size(); i++) {\n int u = edges[i][0];\n in...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\nvoid DFS(queue<int>&q,vector<vector<int>>&ans,vector<int>adj[],int node,vector<int>&visited){\n while(!q.empty()){\n int x=q.front();\n q.pop();\n ans[node].push_back(x);\n for(auto it:adj[x]){\n if(!visited[it]){\n visited[it]=1;\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n void ancestor(vector<vector<int>>&graph,int node,set<int>&ans)\n {\n if(graph[node].size()==0 ) return;\n for(auto &v:graph[node])\n { if(ans.find(v)== ans.end())\n {\n ans.insert(v);\n ancestor(graph,v,ans);\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) \n {\n vector<vector<int>> ancestors(n);\n unordered_map<int, unordered_set<int>> graph;\n\n for (auto x: edges)\n graph[x[1]].insert(x[0]);\n \n for (int i = ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n void dfs(vector<vector<int>>&adj, vector<set<int>>&ans, vector<bool>&vis, int nod)\n { \n vis[nod] = true;\n for(int i=0; i < adj[nod].size(); i++)\n {\n if(vis[adj[nod][i]] == false)\n {\n dfs(adj, ans, vis, adj[...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\n void dfs(vector<int>& result, int node, vector<vector<int>>& graph) {\n set<int> seen;\n stack<int> S;\n S.push(node);\n seen.insert(node);\n while(!S.empty()) {\n int curr = S.top();\n S.pop();\n for(int n : graph[cu...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n void sol(int j,int val,vector<int>&v2,vector<vector<int>>&v,map<int,vector<int>>&mp,unordered_set<int>&st,int cnt){\n if(mp[val].size()==0){\n return;\n }\n if(mp[val].size()!=0){\n for(auto& ch:mp[val]){\n if(st.find(...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n \n //Need to construct an adjacency list\n vector<vector<int>> adjList;\n unordered_map<int, unordered_set<int>> ancList;\n for (int i = 0; i < n; i++)\n {\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n void calldfs(int node ,int i, vector<vector<int>>&result,map<int,set<int>>&mp, map<int,bool>&visit){\n visit[i]=true;\n for(auto it: mp[i]){\n if(visit[it]!=true){\n result[it].push_back(node);\n calldfs(node,it,result,mp...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<int>adj[n];\n vector<int>indegree(n,0);\n for(auto it : edges){\n adj[it[0]].push_back(it[1]);\n indegree[it[1]]++;\n }\n vector<set<int>>te...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<int>adj[n];\n vector<int>indegree(n,0);\n for(auto it : edges){\n adj[it[0]].push_back(it[1]);\n indegree[it[1]]++;\n }\n vector<set<int>>te...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "#include <map>\n#include <algorithm>\n\nclass Solution {\npublic:\n vector<int> getParentsOfNode(int id)\n {\n if (parentsOfNode.contains(id))\n return parentsOfNode[id];\n\n unordered_set<int> result;\n for (const auto& incomingId : incomingEdges[id])\n {\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<int>tmp;\n set<int>visited;\n vector<int> fetch(int src,vector<list<int>>&adj){\n // int x=src;\n // vector<int>ans;\n queue<int>q;\n q.push(src);\n visited.insert(src);\n while(q.size()){\n int n=q.front();\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n \n vector<int> dfs(int i, vector< vector<int> > &v,vector< vector<int> > &res, vector<bool> &visited){\n vector<int> temp,cum;\n set<int> s;\n visited[i] = true;\n \n for(auto j: v[i]){\n s.insert(j);\n \n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n unordered_map<int, vector<int>>dp;\n\n vector<int> getAncestors(int node, unordered_map<int, vector<int>>& parentMap) {\n if (dp.find(node) != dp.end()) {\n return dp[node];\n }\n unordered_set<int> ancestors;\n for (int parent: paren...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\nvector<int> Edges(vector<int>adj[],int i)\n{\n set<int> temp;\n queue<int> st;\n st.push(i);\n while (!st.empty()) {\n int node = st.front();\n st.pop();\n for (auto it : adj[node]) {\n if (temp.find(it) == t...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> ancestors;\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> parents(n,vector<int>());\n ancestors = vector<vector<int>>(n,vector<int>());\n\n for(auto& edge: edges){\n parents[...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> adjacent;\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> ans(n);\n adjacent.resize(n);\n for(vector<int>& e : edges) {\n adjacent[e[1]].push_back(e[0]);\n }\n f...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Graph {\n vector<vector<int>> adjacencyList;\npublic:\n Graph(int n, vector<vector<int>>& edges) : adjacencyList(n) {\n for (vector<int>& edge : edges) {\n adjacencyList[edge[0]].push_back(edge[1]);\n }\n }\n vector<int>& getNeighbors(int node) {\n return a...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n unordered_map<int, vector<int>> mp;\n unordered_map<int, vector<int>> m;\n vector<vector<int>> res;\n for(auto& el: edges)\n {\n mp[el[1]].push_back(el[0]);\n...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<int> dfs(int node, vector<int> adj[], vector<int> &vis, vector<vector<int>> &ans) {\n \n if(vis[node]) return ans[node];\n vis[node] = 1;\n \n set<int> curr_ans;\n for(auto nbr: adj[node]) {\n vector<int> temp = dfs(...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> adj(n);\n vector<int> indegree(n);\n for(auto e: edges){\n adj[e[1]].push_back(e[0]);\n indegree[e[0]]++;\n }\n vector<int> sta...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> adj(n);\n for(auto it:edges){\n int x = it[0];\n int y = it[1];\n adj[y].push_back(x);\n }\n vector<vector<int>> ans(n);\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "#include <vector>\n#include <queue>\n#include <set>\n#include <algorithm>\n\nusing namespace std;\n\nclass Solution {\npublic:\n std::vector<std::vector<int>> getAncestors(int n, std::vector<std::vector<int>>& edges) {\n // Step 1: Build the graph (adjacency list)\n std::vector<std::vector...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>>parent(n);\n vector<vector<int>>graph(n);\n // for(int i=0;i<edges.size();i++){\n // graph[edges[i][0]].push_back()\n // }\n for(int i=0;i<...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> ans(n, vector<int>());\n\n helper(n, edges, ans);\n\n return ans;\n }\n\n void helper(int n, vector<vector<int>>& edges, vector<vector<int>>& ans)\n {\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n vector<vector<int>> ans(n, vector<int>());\n\n helper(n, edges, ans);\n\n return ans;\n }\n\n void helper(int n, vector<vector<int>>& edges, vector<vector<int>>& ans)\n {\n ...
1,431
<p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p> <p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = ...
3
{ "code": "typedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<bool> vb;\n\nclass Solution {\npublic:\n void tpsort(int node, map<int, vi>& m, stack<int>& s, vb& visited, set<int>& res) {\n visited[node] = true;\n for (auto d: m[node]) {\n // if not visited\n if (!v...