forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
28 lines (24 loc) · 920 Bytes
/
Solution.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
impl Solution {
pub fn valid_path(n: i32, edges: Vec<Vec<i32>>, source: i32, destination: i32) -> bool {
let mut disjoint_set: Vec<i32> = vec![0; n as usize];
// Initialize the set
for i in 0..n {
disjoint_set[i as usize] = i;
}
// Traverse the edges
for p_vec in &edges {
let parent_one = Solution::find(p_vec[0], &mut disjoint_set);
let parent_two = Solution::find(p_vec[1], &mut disjoint_set);
disjoint_set[parent_one as usize] = parent_two;
}
let p_s = Solution::find(source, &mut disjoint_set);
let p_d = Solution::find(destination, &mut disjoint_set);
p_s == p_d
}
pub fn find(x: i32, d_set: &mut Vec<i32>) -> i32 {
if d_set[x as usize] != x {
d_set[x as usize] = Solution::find(d_set[x as usize], d_set);
}
d_set[x as usize]
}
}