1 Star 0 Fork 0

Neo_1001/LeetCode刷题记录

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LC_1971.java 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
Neo_1001 提交于 2024-04-10 22:16 . first commit
package LeetCode;
public class LC_1971 {
public static void main(String[] args) {
int[][] edges = {{0, 1}, {0, 2}, {3, 5}, {5, 4}, {4, 3}};
System.out.println(validPath(6, edges, 3, 2));
}
public static boolean validPath(int n, int[][] edges, int source, int destination) {
int[] fa = new int[n];
init(fa);
for (int i = 0; i < edges.length; i++) {
join(fa, edges[i][0], edges[i][1]);
}
return isSame(fa, source, destination);
}
public static void init(int[] fa) {
for (int i = 0; i < fa.length; i++) {
fa[i] = i;
}
}
public static void join(int[] fa, int u, int v) {
u = find(fa, u);
v = find(fa, v);
if (u == v) return;
fa[v] = u;
}
public static int find(int[] fa, int x) {
if (x == fa[x]) return x;
else {
fa[x] = find(fa, fa[x]);
return fa[x];
}
}
public static boolean isSame(int[] fa, int u, int v) {
u = find(fa, u);
v = find(fa, v);
return u == v;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/neo1001/leet-code-practice-record.git
git@gitee.com:neo1001/leet-code-practice-record.git
neo1001
leet-code-practice-record
LeetCode刷题记录
master

搜索帮助