Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2192 #2535

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -234,6 +234,50 @@ function getAncestors(n: number, edges: number[][]): number[][] {
}
```

```cs
public class Solution {
private int n;
private List<int>[] g;
private IList<IList<int>> ans;

public IList<IList<int>> GetAncestors(int n, int[][] edges) {
g = new List<int>[n];
this.n = n;
for (int i = 0; i < n; i++) {
g[i] = new List<int>();
}
foreach (var e in edges) {
g[e[0]].Add(e[1]);
}
ans = new List<IList<int>>();
for (int i = 0; i < n; ++i) {
ans.Add(new List<int>());
}
for (int i = 0; i < n; ++i) {
BFS(i);
}
return ans;
}

private void BFS(int s) {
Queue<int> q = new Queue<int>();
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);
}
}
}
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

Expand Down Expand Up @@ -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;
Expand All @@ -222,6 +228,50 @@ function getAncestors(n: number, edges: number[][]): number[][] {
}
```

```cs
public class Solution {
private int n;
private List<int>[] g;
private IList<IList<int>> ans;

public IList<IList<int>> GetAncestors(int n, int[][] edges) {
g = new List<int>[n];
this.n = n;
for (int i = 0; i < n; i++) {
g[i] = new List<int>();
}
foreach (var e in edges) {
g[e[0]].Add(e[1]);
}
ans = new List<IList<int>>();
for (int i = 0; i < n; ++i) {
ans.Add(new List<int>());
}
for (int i = 0; i < n; ++i) {
BFS(i);
}
return ans;
}

private void BFS(int s) {
Queue<int> q = new Queue<int>();
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);
}
}
}
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class Solution {
private int n;
private List<int>[] g;
private IList<IList<int>> ans;

public IList<IList<int>> GetAncestors(int n, int[][] edges) {
g = new List<int>[n];
this.n = n;
for (int i = 0; i < n; i++) {
g[i] = new List<int>();
}
foreach (var e in edges) {
g[e[0]].Add(e[1]);
}
ans = new List<IList<int>>();
for (int i = 0; i < n; ++i) {
ans.Add(new List<int>());
}
for (int i = 0; i < n; ++i) {
BFS(i);
}
return ans;
}

private void BFS(int s) {
Queue<int> q = new Queue<int>();
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);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading