前言
2023-9-26 14:38:02
以下内容源自《【学习算法】》
仅供学习交流使用
推荐
并查集的实现
package test1;// 并查集模版(牛客) // 路径压缩 + 小挂大 // 测试链接 : https://www.nowcoder.com/practice/e7ed657974934a30b2010046536a5372 // 请同学们务必参考如下代码中关于输入、输出的处理 // 这是输入输出处理效率很高的写法 // 提交以下的code,提交时请把类名改成"Main",可以直接通过 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; class Code01_UnionFindNowCoder { public static int MAXN = 1000001; public static int[] father = new int[MAXN]; public static int[] size = new int[MAXN]; public static int[] stack = new int[MAXN]; public static int n; public static void build() { for (int i = 0; i <= n; i++) { father[i] = i; size[i] = 1; } } // i号节点,往上一直找,找到代表节点返回! public static int find(int i) { // 沿途收集了几个点 int size = 0; while (i != father[i]) { stack[size++] = i; i = father[i]; } // 沿途节点收集好了,i已经跳到代表节点了 while (size > 0) { father[stack[--size]] = i; } return i; } public static boolean isSameSet(int x, int y) { return find(x) == find(y); } public static void union(int x, int y) { int fx = find(x); int fy = find(y); if (fx != fy) { // fx是集合的代表:拿大小 // fy是集合的代表:拿大小 if (size[fx] >= size[fy]) { size[fx] += size[fy]; father[fy] = fx; } else { size[fy] += size[fx]; father[fx] = fy; } } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StreamTokenizer in = new StreamTokenizer(br); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); while (in.nextToken() != StreamTokenizer.TT_EOF) { n = (int) in.nval; build(); in.nextToken(); int m = (int) in.nval; for (int i = 0; i < m; i++) { in.nextToken(); int op = (int) in.nval; in.nextToken(); int x = (int) in.nval; in.nextToken(); int y = (int) in.nval; if (op == 1) { out.println(isSameSet(x, y) ? "Yes" : "No"); } else { union(x, y); } } } out.flush(); out.close(); br.close(); } }
最后
我们都有光明的未来
祝大家考研上岸
祝大家工作顺利
祝大家得偿所愿
祝大家如愿以偿
点赞收藏关注哦