Skip to content

feat: add solutions to lc problem: No.1971 #2900

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

Merged
merged 18 commits into from
May 24, 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
393 changes: 274 additions & 119 deletions solution/1900-1999/1971.Find if Path Exists in Graph/README.md

Large diffs are not rendered by default.

338 changes: 308 additions & 30 deletions solution/1900-1999/1971.Find if Path Exists in Graph/README_EN.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ class Solution {
public:
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
vector<bool> vis(n);
vector<vector<int>> g(n);
vector<int> g[n];
for (auto& e : edges) {
int a = e[0], b = e[1];
g[a].emplace_back(b);
g[b].emplace_back(a);
}
function<bool(int)> dfs = [&](int i) -> bool {
if (i == destination) return true;
if (i == destination) {
return true;
}
vis[i] = true;
for (int& j : g[i]) {
if (!vis[j] && dfs(j)) {
Expand All @@ -20,4 +22,4 @@ class Solution {
};
return dfs(source);
}
};
};
18 changes: 10 additions & 8 deletions solution/1900-1999/1971.Find if Path Exists in Graph/Solution.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
class Solution {
private int destination;
private boolean[] vis;
private List<Integer>[] g;

public boolean validPath(int n, int[][] edges, int source, int destination) {
vis = new boolean[n];
g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (var e : edges) {
int a = e[0], b = e[1];
g[a].add(b);
g[b].add(a);
}
return dfs(source, destination);
vis = new boolean[n];
this.destination = destination;
return dfs(source);
}

private boolean dfs(int source, int destination) {
if (source == destination) {
private boolean dfs(int i) {
if (i == destination) {
return true;
}
vis[source] = true;
for (int nxt : g[source]) {
if (!vis[nxt] && dfs(nxt, destination)) {
vis[i] = true;
for (int j : g[i]) {
if (!vis[j] && dfs(j)) {
return true;
}
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def dfs(i):
return True
return False

g = defaultdict(list)
g = [[] for _ in range(n)]
for a, b in edges:
g[a].append(b)
g[b].append(a)
Expand Down
22 changes: 22 additions & 0 deletions solution/1900-1999/1971.Find if Path Exists in Graph/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function validPath(n: number, edges: number[][], source: number, destination: number): boolean {
const g: number[][] = Array.from({ length: n }, () => []);
for (const [a, b] of edges) {
g[a].push(b);
g[b].push(a);
}

const vis = new Set<number>();
const dfs = (i: number) => {
if (i === destination) {
return true;
}
if (vis.has(i)) {
return false;
}

vis.add(i);
return g[i].some(dfs);
};

return dfs(source);
}
33 changes: 24 additions & 9 deletions solution/1900-1999/1971.Find if Path Exists in Graph/Solution2.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
class Solution {
public:
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
vector<int> p(n);
iota(p.begin(), p.end(), 0);
function<int(int)> find = [&](int x) -> int {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
};
for (auto& e : edges) p[find(e[0])] = find(e[1]);
return find(source) == find(destination);
vector<vector<int>> g(n);
for (auto& e : edges) {
int a = e[0], b = e[1];
g[a].push_back(b);
g[b].push_back(a);
}
queue<int> q{{source}};
vector<bool> vis(n);
vis[source] = true;
while (q.size()) {
int i = q.front();
q.pop();
if (i == destination) {
return true;
}
for (int j : g[i]) {
if (!vis[j]) {
vis[j] = true;
q.push(j);
}
}
}
return false;
}
};
};
34 changes: 21 additions & 13 deletions solution/1900-1999/1971.Find if Path Exists in Graph/Solution2.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
func validPath(n int, edges [][]int, source int, destination int) bool {
p := make([]int, n)
for i := range p {
p[i] = i
g := make([][]int, n)
for _, e := range edges {
a, b := e[0], e[1]
g[a] = append(g[a], b)
g[b] = append(g[b], a)
}
var find func(x int) int
find = func(x int) int {
if p[x] != x {
p[x] = find(p[x])
q := []int{source}
vis := make([]bool, n)
vis[source] = true
for len(q) > 0 {
i := q[0]
q = q[1:]
if i == destination {
return true
}
for _, j := range g[i] {
if !vis[j] {
vis[j] = true
q = append(q, j)
}
}
return p[x]
}
for _, e := range edges {
p[find(e[0])] = find(e[1])
}
return find(source) == find(destination)
}
return false
}
39 changes: 23 additions & 16 deletions solution/1900-1999/1971.Find if Path Exists in Graph/Solution2.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
class Solution {
private int[] p;

public boolean validPath(int n, int[][] edges, int source, int destination) {
p = new int[n];
for (int i = 0; i < n; ++i) {
p[i] = i;
List<Integer>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (var e : edges) {
int a = e[0], b = e[1];
g[a].add(b);
g[b].add(a);
}
for (int[] e : edges) {
p[find(e[0])] = find(e[1]);
Deque<Integer> q = new ArrayDeque<>();
q.offer(source);
boolean[] vis = new boolean[n];
vis[source] = true;
while (!q.isEmpty()) {
int i = q.poll();
if (i == destination) {
return true;
}
for (int j : g[i]) {
if (!vis[j]) {
vis[j] = true;
q.offer(j);
}
}
}
return find(source) == find(destination);
return false;
}

private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
}
}
23 changes: 15 additions & 8 deletions solution/1900-1999/1971.Find if Path Exists in Graph/Solution2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ class Solution:
def validPath(
self, n: int, edges: List[List[int]], source: int, destination: int
) -> bool:
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
g = [[] for _ in range(n)]
for a, b in edges:
g[a].append(b)
g[b].append(a)

p = list(range(n))
for u, v in edges:
p[find(u)] = find(v)
return find(source) == find(destination)
q = deque([source])
vis = {source}
while q:
i = q.popleft()
if i == destination:
return True
for j in g[i]:
if j not in vis:
vis.add(j)
q.append(j)
return False
32 changes: 32 additions & 0 deletions solution/1900-1999/1971.Find if Path Exists in Graph/Solution2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::collections::{ HashSet, VecDeque };

impl Solution {
pub fn valid_path(n: i32, edges: Vec<Vec<i32>>, source: i32, destination: i32) -> bool {
let mut g = vec![HashSet::new(); n as usize];
for e in edges {
let a = e[0] as usize;
let b = e[1] as usize;
g[a].insert(b);
g[b].insert(a);
}

let mut q = VecDeque::new();
q.push_back(source as usize);
let mut vis = vec![false; n as usize];
vis[source as usize] = true;

while let Some(i) = q.pop_front() {
if i == (destination as usize) {
return true;
}
for &j in &g[i] {
if !vis[j] {
vis[j] = true;
q.push_back(j);
}
}
}

false
}
}
25 changes: 25 additions & 0 deletions solution/1900-1999/1971.Find if Path Exists in Graph/Solution2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function validPath(n: number, edges: number[][], source: number, destination: number): boolean {
const g: number[][] = Array.from({ length: n }, () => []);

for (const [a, b] of edges) {
g[a].push(b);
g[b].push(a);
}

const vis = new Set<number>();
const q = [source];

while (q.length) {
const i = q.pop()!;
if (i === destination) {
return true;
}
if (vis.has(i)) {
continue;
}
vis.add(i);
q.push(...g[i]);
}

return false;
}
17 changes: 17 additions & 0 deletions solution/1900-1999/1971.Find if Path Exists in Graph/Solution3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
vector<int> p(n);
iota(p.begin(), p.end(), 0);
function<int(int)> find = [&](int x) -> int {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
};
for (auto& e : edges) {
p[find(e[0])] = find(e[1]);
}
return find(source) == find(destination);
}
};
17 changes: 17 additions & 0 deletions solution/1900-1999/1971.Find if Path Exists in Graph/Solution3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
func validPath(n int, edges [][]int, source int, destination int) bool {
p := make([]int, n)
for i := range p {
p[i] = i
}
var find func(x int) int
find = func(x int) int {
if p[x] != x {
p[x] = find(p[x])
}
return p[x]
}
for _, e := range edges {
p[find(e[0])] = find(e[1])
}
return find(source) == find(destination)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
private int[] p;

public boolean validPath(int n, int[][] edges, int source, int destination) {
p = new int[n];
for (int i = 0; i < n; ++i) {
p[i] = i;
}
for (int[] e : edges) {
p[find(e[0])] = find(e[1]);
}
return find(source) == find(destination);
}

private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
}
13 changes: 13 additions & 0 deletions solution/1900-1999/1971.Find if Path Exists in Graph/Solution3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def validPath(
self, n: int, edges: List[List[int]], source: int, destination: int
) -> bool:
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]

p = list(range(n))
for u, v in edges:
p[find(u)] = find(v)
return find(source) == find(destination)
13 changes: 13 additions & 0 deletions solution/1900-1999/1971.Find if Path Exists in Graph/Solution3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function validPath(n: number, edges: number[][], source: number, destination: number): boolean {
const p: number[] = Array.from({ length: n }, (_, i) => i);
const find = (x: number): number => {
if (p[x] !== x) {
p[x] = find(p[x]);
}
return p[x];
};
for (const [a, b] of edges) {
p[find(a)] = find(b);
}
return find(source) === find(destination);
}
Loading