Skip to content

Commit 770693e

Browse files
rain84yanglbme
andauthoredMay 24, 2024··
feat: add solutions to lc problem: No.1971 (doocs#2900)
* feat: ✨ add TS solution to lc problem: No.1971 * Update README_EN.md * Update README.md * Update Solution.py * Update Solution.java * Update Solution.cpp * Update Solution.ts * Update Solution2.py * Update Solution2.java * Update Solution2.cpp * Update Solution2.go * Update Solution2.ts * Create Solution2.rs * Create Solution3.py * Create Solution3.java * Create Solution3.cpp * Create Solution3.go * Create Solution3.ts --------- Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent d9494c8 commit 770693e

17 files changed

+841
-207
lines changed
 

‎solution/1900-1999/1971.Find if Path Exists in Graph/README.md

+274-119
Large diffs are not rendered by default.

‎solution/1900-1999/1971.Find if Path Exists in Graph/README_EN.md

+308-30
Large diffs are not rendered by default.

‎solution/1900-1999/1971.Find if Path Exists in Graph/Solution.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ class Solution {
22
public:
33
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
44
vector<bool> vis(n);
5-
vector<vector<int>> g(n);
5+
vector<int> g[n];
66
for (auto& e : edges) {
77
int a = e[0], b = e[1];
88
g[a].emplace_back(b);
99
g[b].emplace_back(a);
1010
}
1111
function<bool(int)> dfs = [&](int i) -> bool {
12-
if (i == destination) return true;
12+
if (i == destination) {
13+
return true;
14+
}
1315
vis[i] = true;
1416
for (int& j : g[i]) {
1517
if (!vis[j] && dfs(j)) {
@@ -20,4 +22,4 @@ class Solution {
2022
};
2123
return dfs(source);
2224
}
23-
};
25+
};
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
class Solution {
2+
private int destination;
23
private boolean[] vis;
34
private List<Integer>[] g;
45

56
public boolean validPath(int n, int[][] edges, int source, int destination) {
6-
vis = new boolean[n];
77
g = new List[n];
88
Arrays.setAll(g, k -> new ArrayList<>());
99
for (var e : edges) {
1010
int a = e[0], b = e[1];
1111
g[a].add(b);
1212
g[b].add(a);
1313
}
14-
return dfs(source, destination);
14+
vis = new boolean[n];
15+
this.destination = destination;
16+
return dfs(source);
1517
}
1618

17-
private boolean dfs(int source, int destination) {
18-
if (source == destination) {
19+
private boolean dfs(int i) {
20+
if (i == destination) {
1921
return true;
2022
}
21-
vis[source] = true;
22-
for (int nxt : g[source]) {
23-
if (!vis[nxt] && dfs(nxt, destination)) {
23+
vis[i] = true;
24+
for (int j : g[i]) {
25+
if (!vis[j] && dfs(j)) {
2426
return true;
2527
}
2628
}
2729
return false;
2830
}
29-
}
31+
}

‎solution/1900-1999/1971.Find if Path Exists in Graph/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def dfs(i):
1111
return True
1212
return False
1313

14-
g = defaultdict(list)
14+
g = [[] for _ in range(n)]
1515
for a, b in edges:
1616
g[a].append(b)
1717
g[b].append(a)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function validPath(n: number, edges: number[][], source: number, destination: number): boolean {
2+
const g: number[][] = Array.from({ length: n }, () => []);
3+
for (const [a, b] of edges) {
4+
g[a].push(b);
5+
g[b].push(a);
6+
}
7+
8+
const vis = new Set<number>();
9+
const dfs = (i: number) => {
10+
if (i === destination) {
11+
return true;
12+
}
13+
if (vis.has(i)) {
14+
return false;
15+
}
16+
17+
vis.add(i);
18+
return g[i].some(dfs);
19+
};
20+
21+
return dfs(source);
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
class Solution {
22
public:
33
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
4-
vector<int> p(n);
5-
iota(p.begin(), p.end(), 0);
6-
function<int(int)> find = [&](int x) -> int {
7-
if (p[x] != x) p[x] = find(p[x]);
8-
return p[x];
9-
};
10-
for (auto& e : edges) p[find(e[0])] = find(e[1]);
11-
return find(source) == find(destination);
4+
vector<vector<int>> g(n);
5+
for (auto& e : edges) {
6+
int a = e[0], b = e[1];
7+
g[a].push_back(b);
8+
g[b].push_back(a);
9+
}
10+
queue<int> q{{source}};
11+
vector<bool> vis(n);
12+
vis[source] = true;
13+
while (q.size()) {
14+
int i = q.front();
15+
q.pop();
16+
if (i == destination) {
17+
return true;
18+
}
19+
for (int j : g[i]) {
20+
if (!vis[j]) {
21+
vis[j] = true;
22+
q.push(j);
23+
}
24+
}
25+
}
26+
return false;
1227
}
13-
};
28+
};
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
func validPath(n int, edges [][]int, source int, destination int) bool {
2-
p := make([]int, n)
3-
for i := range p {
4-
p[i] = i
2+
g := make([][]int, n)
3+
for _, e := range edges {
4+
a, b := e[0], e[1]
5+
g[a] = append(g[a], b)
6+
g[b] = append(g[b], a)
57
}
6-
var find func(x int) int
7-
find = func(x int) int {
8-
if p[x] != x {
9-
p[x] = find(p[x])
8+
q := []int{source}
9+
vis := make([]bool, n)
10+
vis[source] = true
11+
for len(q) > 0 {
12+
i := q[0]
13+
q = q[1:]
14+
if i == destination {
15+
return true
16+
}
17+
for _, j := range g[i] {
18+
if !vis[j] {
19+
vis[j] = true
20+
q = append(q, j)
21+
}
1022
}
11-
return p[x]
12-
}
13-
for _, e := range edges {
14-
p[find(e[0])] = find(e[1])
1523
}
16-
return find(source) == find(destination)
17-
}
24+
return false
25+
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
class Solution {
2-
private int[] p;
3-
42
public boolean validPath(int n, int[][] edges, int source, int destination) {
5-
p = new int[n];
6-
for (int i = 0; i < n; ++i) {
7-
p[i] = i;
3+
List<Integer>[] g = new List[n];
4+
Arrays.setAll(g, k -> new ArrayList<>());
5+
for (var e : edges) {
6+
int a = e[0], b = e[1];
7+
g[a].add(b);
8+
g[b].add(a);
89
}
9-
for (int[] e : edges) {
10-
p[find(e[0])] = find(e[1]);
10+
Deque<Integer> q = new ArrayDeque<>();
11+
q.offer(source);
12+
boolean[] vis = new boolean[n];
13+
vis[source] = true;
14+
while (!q.isEmpty()) {
15+
int i = q.poll();
16+
if (i == destination) {
17+
return true;
18+
}
19+
for (int j : g[i]) {
20+
if (!vis[j]) {
21+
vis[j] = true;
22+
q.offer(j);
23+
}
24+
}
1125
}
12-
return find(source) == find(destination);
26+
return false;
1327
}
14-
15-
private int find(int x) {
16-
if (p[x] != x) {
17-
p[x] = find(p[x]);
18-
}
19-
return p[x];
20-
}
21-
}
28+
}

‎solution/1900-1999/1971.Find if Path Exists in Graph/Solution2.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ class Solution:
22
def validPath(
33
self, n: int, edges: List[List[int]], source: int, destination: int
44
) -> bool:
5-
def find(x):
6-
if p[x] != x:
7-
p[x] = find(p[x])
8-
return p[x]
5+
g = [[] for _ in range(n)]
6+
for a, b in edges:
7+
g[a].append(b)
8+
g[b].append(a)
99

10-
p = list(range(n))
11-
for u, v in edges:
12-
p[find(u)] = find(v)
13-
return find(source) == find(destination)
10+
q = deque([source])
11+
vis = {source}
12+
while q:
13+
i = q.popleft()
14+
if i == destination:
15+
return True
16+
for j in g[i]:
17+
if j not in vis:
18+
vis.add(j)
19+
q.append(j)
20+
return False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::collections::{ HashSet, VecDeque };
2+
3+
impl Solution {
4+
pub fn valid_path(n: i32, edges: Vec<Vec<i32>>, source: i32, destination: i32) -> bool {
5+
let mut g = vec![HashSet::new(); n as usize];
6+
for e in edges {
7+
let a = e[0] as usize;
8+
let b = e[1] as usize;
9+
g[a].insert(b);
10+
g[b].insert(a);
11+
}
12+
13+
let mut q = VecDeque::new();
14+
q.push_back(source as usize);
15+
let mut vis = vec![false; n as usize];
16+
vis[source as usize] = true;
17+
18+
while let Some(i) = q.pop_front() {
19+
if i == (destination as usize) {
20+
return true;
21+
}
22+
for &j in &g[i] {
23+
if !vis[j] {
24+
vis[j] = true;
25+
q.push_back(j);
26+
}
27+
}
28+
}
29+
30+
false
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function validPath(n: number, edges: number[][], source: number, destination: number): boolean {
2+
const g: number[][] = Array.from({ length: n }, () => []);
3+
4+
for (const [a, b] of edges) {
5+
g[a].push(b);
6+
g[b].push(a);
7+
}
8+
9+
const vis = new Set<number>();
10+
const q = [source];
11+
12+
while (q.length) {
13+
const i = q.pop()!;
14+
if (i === destination) {
15+
return true;
16+
}
17+
if (vis.has(i)) {
18+
continue;
19+
}
20+
vis.add(i);
21+
q.push(...g[i]);
22+
}
23+
24+
return false;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
4+
vector<int> p(n);
5+
iota(p.begin(), p.end(), 0);
6+
function<int(int)> find = [&](int x) -> int {
7+
if (p[x] != x) {
8+
p[x] = find(p[x]);
9+
}
10+
return p[x];
11+
};
12+
for (auto& e : edges) {
13+
p[find(e[0])] = find(e[1]);
14+
}
15+
return find(source) == find(destination);
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func validPath(n int, edges [][]int, source int, destination int) bool {
2+
p := make([]int, n)
3+
for i := range p {
4+
p[i] = i
5+
}
6+
var find func(x int) int
7+
find = func(x int) int {
8+
if p[x] != x {
9+
p[x] = find(p[x])
10+
}
11+
return p[x]
12+
}
13+
for _, e := range edges {
14+
p[find(e[0])] = find(e[1])
15+
}
16+
return find(source) == find(destination)
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
private int[] p;
3+
4+
public boolean validPath(int n, int[][] edges, int source, int destination) {
5+
p = new int[n];
6+
for (int i = 0; i < n; ++i) {
7+
p[i] = i;
8+
}
9+
for (int[] e : edges) {
10+
p[find(e[0])] = find(e[1]);
11+
}
12+
return find(source) == find(destination);
13+
}
14+
15+
private int find(int x) {
16+
if (p[x] != x) {
17+
p[x] = find(p[x]);
18+
}
19+
return p[x];
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def validPath(
3+
self, n: int, edges: List[List[int]], source: int, destination: int
4+
) -> bool:
5+
def find(x):
6+
if p[x] != x:
7+
p[x] = find(p[x])
8+
return p[x]
9+
10+
p = list(range(n))
11+
for u, v in edges:
12+
p[find(u)] = find(v)
13+
return find(source) == find(destination)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function validPath(n: number, edges: number[][], source: number, destination: number): boolean {
2+
const p: number[] = Array.from({ length: n }, (_, i) => i);
3+
const find = (x: number): number => {
4+
if (p[x] !== x) {
5+
p[x] = find(p[x]);
6+
}
7+
return p[x];
8+
};
9+
for (const [a, b] of edges) {
10+
p[find(a)] = find(b);
11+
}
12+
return find(source) === find(destination);
13+
}

0 commit comments

Comments
 (0)