Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 11 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Mar 5, 2025
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
94bdecf
添加474. 一和零 java 三维DP数组实现代码
HelloDam Nov 29, 2024
da742fe
修改0112.路径总和.md的Java版本代码的字母小写问题
curforever Jan 23, 2025
27718a4
docs: 为 0494.目标和.md 完善 JavaDoc 注释, 添加动规五部曲注释, 规范部分代码格式, 修改部分变量名以增强代码语义化
asnsuyu Jan 24, 2025
e6698cb
修改0530.二叉搜索树的最小绝对差.md的Java版本 进行了代码格式化并添加了统一迭代法的注释
curforever Jan 25, 2025
c2e95f6
108冗余连接 java实现
HelloDam Jan 26, 2025
04ad873
Merge branch 'youngyangyang04:master' into master
curforever Feb 3, 2025
333099a
修改二叉树总结篇.md一处列表级别的格式问题
curforever Feb 3, 2025
bb12666
Merge branch 'youngyangyang04:master' into master
asnsuyu Mar 1, 2025
f4addc4
Merge pull request #2874 from curforever/master
youngyangyang04 Mar 5, 2025
bba027e
Merge pull request #2847 from HelloDam/master
youngyangyang04 Mar 5, 2025
e5b75df
Merge pull request #2877 from asnsuyu/master
youngyangyang04 Mar 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
108冗余连接 java实现
  • Loading branch information
HelloDam committed Jan 26, 2025
commit c2e95f6c25b2c3f9ff61dc0891cc08360b676bb1
75 changes: 75 additions & 0 deletions problems/kamacoder/0108.冗余连接.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,81 @@ int main() {

### Java

```java
import java.util.Scanner;

public class Main {
private static int[] father;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int pointNum = scanner.nextInt();
father = new int[pointNum + 1];
init();
for (int i = 0; i < pointNum; i++) {
join(scanner.nextInt(), scanner.nextInt());
}
}

/**
* 并查集初始化
*/
private static void init() {
for (int i = 1; i < father.length; i++) {
// 让每个元素指向自己
father[i] = i;
}
}

/**
* 并查集寻根
*
* @param u
* @return
*/
private static int find(int u) {
// 判断 u 是否等于自己,如果是的话,直接返回自己
// 如果不等于自己,就寻找根,寻找的时候,反复进行路径压缩
return u == father[u] ? u : (father[u] = find(father[u]));
}

/**
* 判断 u 和 v 是否同根
*
* @param u
* @param v
* @return
*/
private static boolean isSame(int u, int v) {
return find(u) == find(v);
}

/**
* 添加 边 到并查集,v 指向 u
*
* @param u
* @param v
*/
private static void join(int u, int v) {
// --if-- 如果两个点已经同根,说明他们的信息已经存储到并查集中了,直接返回即可
// 寻找u的根
int uRoot = find(u);
// 寻找v的根
int vRoot = find(v);
if (uRoot == vRoot) {
// --if-- 如果u,v的根相同,说明两者已经连接了,直接输出
System.out.println(u + " " + v);
return;
}
// --if-- 将信息添加到并查集
father[vRoot] = uRoot;
}

}
```



### Python

```python
Expand Down

AltStyle によって変換されたページ (->オリジナル) /