-
Notifications
You must be signed in to change notification settings - Fork 45
/
_353.java
35 lines (30 loc) · 922 Bytes
/
_353.java
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
/**
* LeetCode 261 - Graph Valid Tree
* <p>
* Union-find Set
*/
public class _353 {
public boolean validTree(int n, int[][] edges) {
if (edges.length != n - 1) return false;
class UnionFindSet {
final int[] parent;
public UnionFindSet(int n) {
parent = new int[n];
for (int i = 0; i < n; i++) parent[i] = i;
}
public int find(int i) {
if (parent[i] != i) parent[i] = find(parent[i]);
return parent[i];
}
public void union(int i, int j) {
parent[find(i)] = parent[find(j)];
}
}
UnionFindSet set = new UnionFindSet(n);
for (int[] edge : edges)
set.union(edge[0], edge[1]);
for (int i = 0; i < n; i++)
if (set.find(0) != set.find(i)) return false;
return true;
}
}