Skip to content

Commit 5a4a9e6

Browse files
authored
feat: add solutions to lc problem: No.2192 (doocs#2535)
No.2192.All Ancestors of a Node in a Directed Acyclic Graph
1 parent fa17fec commit 5a4a9e6

File tree

4 files changed

+139
-4
lines changed

4 files changed

+139
-4
lines changed

solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ function getAncestors(n: number, edges: number[][]): number[][] {
217217
const vis: boolean[] = Array.from({ length: n }, () => false);
218218
vis[s] = true;
219219
while (q.length) {
220-
const i = q.shift()!;
220+
const i = q.pop()!;
221221
for (const j of g[i]) {
222222
if (!vis[j]) {
223223
vis[j] = true;
@@ -234,6 +234,50 @@ function getAncestors(n: number, edges: number[][]): number[][] {
234234
}
235235
```
236236

237+
```cs
238+
public class Solution {
239+
private int n;
240+
private List<int>[] g;
241+
private IList<IList<int>> ans;
242+
243+
public IList<IList<int>> GetAncestors(int n, int[][] edges) {
244+
g = new List<int>[n];
245+
this.n = n;
246+
for (int i = 0; i < n; i++) {
247+
g[i] = new List<int>();
248+
}
249+
foreach (var e in edges) {
250+
g[e[0]].Add(e[1]);
251+
}
252+
ans = new List<IList<int>>();
253+
for (int i = 0; i < n; ++i) {
254+
ans.Add(new List<int>());
255+
}
256+
for (int i = 0; i < n; ++i) {
257+
BFS(i);
258+
}
259+
return ans;
260+
}
261+
262+
private void BFS(int s) {
263+
Queue<int> q = new Queue<int>();
264+
q.Enqueue(s);
265+
bool[] vis = new bool[n];
266+
vis[s] = true;
267+
while (q.Count > 0) {
268+
int i = q.Dequeue();
269+
foreach (int j in g[i]) {
270+
if (!vis[j]) {
271+
vis[j] = true;
272+
q.Enqueue(j);
273+
ans[j].Add(s);
274+
}
275+
}
276+
}
277+
}
278+
}
279+
```
280+
237281
<!-- tabs:end -->
238282

239283
<!-- end -->

solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/README_EN.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ The above diagram represents the input graph.
5959

6060
## Solutions
6161

62-
### Solution 1
62+
### Solution 1: BFS
63+
64+
First, we construct the adjacency list $g$ based on the two-dimensional array $edges$, where $g[i]$ represents all successor nodes of node $i$.
65+
66+
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.
67+
68+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Where $n$ is the number of nodes.
6369

6470
<!-- tabs:start -->
6571

@@ -205,7 +211,7 @@ function getAncestors(n: number, edges: number[][]): number[][] {
205211
const vis: boolean[] = Array.from({ length: n }, () => false);
206212
vis[s] = true;
207213
while (q.length) {
208-
const i = q.shift()!;
214+
const i = q.pop()!;
209215
for (const j of g[i]) {
210216
if (!vis[j]) {
211217
vis[j] = true;
@@ -222,6 +228,50 @@ function getAncestors(n: number, edges: number[][]): number[][] {
222228
}
223229
```
224230

231+
```cs
232+
public class Solution {
233+
private int n;
234+
private List<int>[] g;
235+
private IList<IList<int>> ans;
236+
237+
public IList<IList<int>> GetAncestors(int n, int[][] edges) {
238+
g = new List<int>[n];
239+
this.n = n;
240+
for (int i = 0; i < n; i++) {
241+
g[i] = new List<int>();
242+
}
243+
foreach (var e in edges) {
244+
g[e[0]].Add(e[1]);
245+
}
246+
ans = new List<IList<int>>();
247+
for (int i = 0; i < n; ++i) {
248+
ans.Add(new List<int>());
249+
}
250+
for (int i = 0; i < n; ++i) {
251+
BFS(i);
252+
}
253+
return ans;
254+
}
255+
256+
private void BFS(int s) {
257+
Queue<int> q = new Queue<int>();
258+
q.Enqueue(s);
259+
bool[] vis = new bool[n];
260+
vis[s] = true;
261+
while (q.Count > 0) {
262+
int i = q.Dequeue();
263+
foreach (int j in g[i]) {
264+
if (!vis[j]) {
265+
vis[j] = true;
266+
q.Enqueue(j);
267+
ans[j].Add(s);
268+
}
269+
}
270+
}
271+
}
272+
}
273+
```
274+
225275
<!-- tabs:end -->
226276

227277
<!-- end -->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
public class Solution {
2+
private int n;
3+
private List<int>[] g;
4+
private IList<IList<int>> ans;
5+
6+
public IList<IList<int>> GetAncestors(int n, int[][] edges) {
7+
g = new List<int>[n];
8+
this.n = n;
9+
for (int i = 0; i < n; i++) {
10+
g[i] = new List<int>();
11+
}
12+
foreach (var e in edges) {
13+
g[e[0]].Add(e[1]);
14+
}
15+
ans = new List<IList<int>>();
16+
for (int i = 0; i < n; ++i) {
17+
ans.Add(new List<int>());
18+
}
19+
for (int i = 0; i < n; ++i) {
20+
BFS(i);
21+
}
22+
return ans;
23+
}
24+
25+
private void BFS(int s) {
26+
Queue<int> q = new Queue<int>();
27+
q.Enqueue(s);
28+
bool[] vis = new bool[n];
29+
vis[s] = true;
30+
while (q.Count > 0) {
31+
int i = q.Dequeue();
32+
foreach (int j in g[i]) {
33+
if (!vis[j]) {
34+
vis[j] = true;
35+
q.Enqueue(j);
36+
ans[j].Add(s);
37+
}
38+
}
39+
}
40+
}
41+
}

solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/Solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function getAncestors(n: number, edges: number[][]): number[][] {
99
const vis: boolean[] = Array.from({ length: n }, () => false);
1010
vis[s] = true;
1111
while (q.length) {
12-
const i = q.shift()!;
12+
const i = q.pop()!;
1313
for (const j of g[i]) {
1414
if (!vis[j]) {
1515
vis[j] = true;

0 commit comments

Comments
 (0)