题解:并查集
并查集模板题
- 判断两个点是否在同一个连通块
class Solution {
int[] p = new int[200010];
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
// 并查集
public boolean validPath(int n, int[][] edges, int source, int destination) {
for (int i = 0; i < n; i++) {
p[i] = i;
}
for (int i = 0; i < edges.length; i++) {
int[] edge = edges[i];
int x = find(edge[0]);
int y = find(edge[1]);
if (x != y) {
p[x] = y;
}
}
return find(source) == find(destination);
}
}
``
标签:路径,int,查集,1971,edge,edges,图中,find
From: https://www.cnblogs.com/eiffelzero/p/16992793.html