diff --git a/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/README.md b/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/README.md index fc6c07bc53eba..450114cccacc5 100644 --- a/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/README.md +++ b/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/README.md @@ -217,7 +217,7 @@ function getAncestors(n: number, edges: number[][]): number[][] { const vis: boolean[] = Array.from({ length: n }, () => false); vis[s] = true; while (q.length) { - const i = q.shift()!; + const i = q.pop()!; for (const j of g[i]) { if (!vis[j]) { vis[j] = true; @@ -234,6 +234,50 @@ function getAncestors(n: number, edges: number[][]): number[][] { } ``` +```cs +public class Solution { + private int n; + private List[] g; + private IList> ans; + + public IList> GetAncestors(int n, int[][] edges) { + g = new List[n]; + this.n = n; + for (int i = 0; i < n; i++) { + g[i] = new List(); + } + foreach (var e in edges) { + g[e[0]].Add(e[1]); + } + ans = new List>(); + for (int i = 0; i < n; ++i) { + ans.Add(new List()); + } + for (int i = 0; i < n; ++i) { + BFS(i); + } + return ans; + } + + private void BFS(int s) { + Queue q = new Queue(); + q.Enqueue(s); + bool[] vis = new bool[n]; + vis[s] = true; + while (q.Count > 0) { + int i = q.Dequeue(); + foreach (int j in g[i]) { + if (!vis[j]) { + vis[j] = true; + q.Enqueue(j); + ans[j].Add(s); + } + } + } + } +} +``` + diff --git a/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/README_EN.md b/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/README_EN.md index 2e4226d2c9b2d..e7dfc9fd83518 100644 --- a/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/README_EN.md +++ b/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/README_EN.md @@ -59,7 +59,13 @@ The above diagram represents the input graph. ## Solutions -### Solution 1 +### Solution 1: BFS + +First, we construct the adjacency list $g$ based on the two-dimensional array $edges$, where $g[i]$ represents all successor nodes of node $i$. + +Then, we enumerate node $i$ as the ancestor node from small to large, use BFS to search all successor nodes of node $i$, and add node $i$ to the ancestor list of these successor nodes. + +The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Where $n$ is the number of nodes. @@ -205,7 +211,7 @@ function getAncestors(n: number, edges: number[][]): number[][] { const vis: boolean[] = Array.from({ length: n }, () => false); vis[s] = true; while (q.length) { - const i = q.shift()!; + const i = q.pop()!; for (const j of g[i]) { if (!vis[j]) { vis[j] = true; @@ -222,6 +228,50 @@ function getAncestors(n: number, edges: number[][]): number[][] { } ``` +```cs +public class Solution { + private int n; + private List[] g; + private IList> ans; + + public IList> GetAncestors(int n, int[][] edges) { + g = new List[n]; + this.n = n; + for (int i = 0; i < n; i++) { + g[i] = new List(); + } + foreach (var e in edges) { + g[e[0]].Add(e[1]); + } + ans = new List>(); + for (int i = 0; i < n; ++i) { + ans.Add(new List()); + } + for (int i = 0; i < n; ++i) { + BFS(i); + } + return ans; + } + + private void BFS(int s) { + Queue q = new Queue(); + q.Enqueue(s); + bool[] vis = new bool[n]; + vis[s] = true; + while (q.Count > 0) { + int i = q.Dequeue(); + foreach (int j in g[i]) { + if (!vis[j]) { + vis[j] = true; + q.Enqueue(j); + ans[j].Add(s); + } + } + } + } +} +``` + diff --git a/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/Solution.cs b/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/Solution.cs new file mode 100644 index 0000000000000..528b78034c0bd --- /dev/null +++ b/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/Solution.cs @@ -0,0 +1,41 @@ +public class Solution { + private int n; + private List[] g; + private IList> ans; + + public IList> GetAncestors(int n, int[][] edges) { + g = new List[n]; + this.n = n; + for (int i = 0; i < n; i++) { + g[i] = new List(); + } + foreach (var e in edges) { + g[e[0]].Add(e[1]); + } + ans = new List>(); + for (int i = 0; i < n; ++i) { + ans.Add(new List()); + } + for (int i = 0; i < n; ++i) { + BFS(i); + } + return ans; + } + + private void BFS(int s) { + Queue q = new Queue(); + q.Enqueue(s); + bool[] vis = new bool[n]; + vis[s] = true; + while (q.Count > 0) { + int i = q.Dequeue(); + foreach (int j in g[i]) { + if (!vis[j]) { + vis[j] = true; + q.Enqueue(j); + ans[j].Add(s); + } + } + } + } +} \ No newline at end of file diff --git a/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/Solution.ts b/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/Solution.ts index 55a7ff69c6fb6..61ced4a458bc1 100644 --- a/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/Solution.ts +++ b/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/Solution.ts @@ -9,7 +9,7 @@ function getAncestors(n: number, edges: number[][]): number[][] { const vis: boolean[] = Array.from({ length: n }, () => false); vis[s] = true; while (q.length) { - const i = q.shift()!; + const i = q.pop()!; for (const j of g[i]) { if (!vis[j]) { vis[j] = true;