当前位置: 首页> 教育> 锐评 > 深圳市宝安区教育局官网_网上可以自学什么技术_2023年8月疫情爆发_产品推广公司

深圳市宝安区教育局官网_网上可以自学什么技术_2023年8月疫情爆发_产品推广公司

时间:2025/7/11 15:17:50来源:https://blog.csdn.net/qq_51792424/article/details/144746717 浏览次数:0次
深圳市宝安区教育局官网_网上可以自学什么技术_2023年8月疫情爆发_产品推广公司

108.冗余连接

题目链接:108. 冗余连接

import java.util.*;public class Main{public static void main(String[] args) {int N;Scanner scanner = new Scanner(System.in);N = scanner.nextInt();DisJoint disJoint = new DisJoint(N + 1);for (int i = 0; i < N; ++i) {int s = scanner.nextInt();int t = scanner.nextInt();if(disJoint.isSame(s,t)) {System.out.println(s + " " + t);return;} else {disJoint.join(s,t);}}}}//并查集模板
class DisJoint{private int[] father;public DisJoint(int N) {father = new int[N];for (int i = 0; i < N; ++i){father[i] = i;}}public int find(int n) {return n == father[n] ? n : (father[n] = find(father[n]));}public void join (int n, int m) {n = find(n);m = find(m);if (n == m) return;father[m] = n;}public boolean isSame(int n, int m){n = find(n);m = find(m);return n == m;}}

109.冗余连接II

题目链接:109. 冗余连接II

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class Main {static int n;static int[] father = new int[1001]; // 并查集数组// 并查集初始化public static void init() {for (int i = 1; i <= n; ++i) {father[i] = i;}}// 并查集里寻根的过程public static int find(int u) {if (u == father[u]) return u;return father[u] = find(father[u]); // 路径压缩}// 将 v->u 这条边加入并查集public static void join(int u, int v) {u = find(u);v = find(v);if (u != v) {father[v] = u; // 合并两棵树}}// 判断 u 和 v 是否有同一个根public static boolean same(int u, int v) {return find(u) == find(v);}// 在有向图里找到删除的那条边,使其变成树public static void getRemoveEdge(List<int[]> edges) {init(); // 初始化并查集for (int i = 0; i < n; i++) { // 遍历所有的边if (same(edges.get(i)[0], edges.get(i)[1])) { // 如果构成有向环了,就是要删除的边System.out.println(edges.get(i)[0] + " " + edges.get(i)[1]);return;} else {join(edges.get(i)[0], edges.get(i)[1]);}}}// 删一条边之后判断是不是树public static boolean isTreeAfterRemoveEdge(List<int[]> edges, int deleteEdge) {init(); // 初始化并查集for (int i = 0; i < n; i++) {if (i == deleteEdge) continue;if (same(edges.get(i)[0], edges.get(i)[1])) { // 如果构成有向环了,一定不是树return false;}join(edges.get(i)[0], edges.get(i)[1]);}return true;}public static void main(String[] args) {Scanner sc = new Scanner(System.in);List<int[]> edges = new ArrayList<>(); // 存储所有的边n = sc.nextInt(); // 顶点数int[] inDegree = new int[n + 1]; // 记录每个节点的入度for (int i = 0; i < n; i++) {int s = sc.nextInt(); // 边的起点int t = sc.nextInt(); // 边的终点inDegree[t]++;edges.add(new int[]{s, t}); // 将边加入列表}List<Integer> vec = new ArrayList<>(); // 记录入度为2的边(如果有的话就两条边)// 找入度为2的节点所对应的边,注意要倒序,因为优先删除最后出现的一条边for (int i = n - 1; i >= 0; i--) {if (inDegree[edges.get(i)[1]] == 2) {vec.add(i);}}// 情况一、情况二if (vec.size() > 0) {// vec里的边已经按照倒叙放的,所以优先删 vec.get(0) 这条边if (isTreeAfterRemoveEdge(edges, vec.get(0))) {System.out.println(edges.get(vec.get(0))[0] + " " + edges.get(vec.get(0))[1]);} else {System.out.println(edges.get(vec.get(1))[0] + " " + edges.get(vec.get(1))[1]);}return;}// 处理情况三:明确没有入度为2的情况,一定有有向环,找到构成环的边返回即可getRemoveEdge(edges);}
}

关键字:深圳市宝安区教育局官网_网上可以自学什么技术_2023年8月疫情爆发_产品推广公司

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: