-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1971.java
More file actions
71 lines (61 loc) · 1.81 KB
/
LeetCode1971.java
File metadata and controls
71 lines (61 loc) · 1.81 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class LeetCode1971 {
public static void main(String[] args) {
int n;
int source;
int destination;
int[][] edges;
// 输入:n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
// 输出:true
n = 3;
edges = new int[][] { { 0, 1 }, { 1, 2 }, { 2, 0 } };
source = 0;
destination = 2;
System.out.println(new Solution1971().validPath(n, edges, source, destination));
// 输入:n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination=5
// 输出:false
n = 6;
edges = new int[][] { { 0, 1 }, { 0, 2 }, { 3, 5 }, { 5, 4 }, { 4, 3 } };
source = 0;
destination = 5;
System.out.println(new Solution1971().validPath(n, edges, source, destination));
}
}
class Solution1971 {
int[] parent;
int[] rank;
public boolean validPath(int n, int[][] edges, int source, int destination) {
parent = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 1;
}
for (int[] info : edges) {
int p = info[0];
int q = info[1];
union(p, q);
}
return find(source) == find(destination);
}
private int find(int p) {
while (parent[p] != p) {
p = parent[p];
}
return p;
}
private void union(int p, int q) {
int pRoot = find(p);
int qRoot = find(q);
if (pRoot == qRoot) {
return;
}
if (rank[pRoot] >= rank[qRoot]) {
parent[qRoot] = pRoot;
if (rank[pRoot] == rank[qRoot]) {
rank[pRoot] = rank[pRoot] + 1;
}
} else {
parent[pRoot] = qRoot;
}
}
}