mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
perf:封装成类,理解起来更容易
This commit is contained in:
@ -250,105 +250,131 @@ int main() {
|
||||
## 其他语言版本
|
||||
|
||||
### Java
|
||||
|
||||
```java
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.*;
|
||||
|
||||
/*
|
||||
* 冗余连接II。主要问题是存在入度为2或者成环,也可能两个问题同时存在。
|
||||
* 1.判断入度为2的边
|
||||
* 2.判断是否成环(并查集)
|
||||
*/
|
||||
|
||||
public class Main {
|
||||
static int n;
|
||||
static int[] father = new int[1001]; // 并查集数组
|
||||
/**
|
||||
* 并查集模板
|
||||
*/
|
||||
static class Disjoint {
|
||||
|
||||
// 并查集初始化
|
||||
public static void init() {
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
father[i] = i;
|
||||
}
|
||||
}
|
||||
private final int[] father;
|
||||
|
||||
// 并查集里寻根的过程
|
||||
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 Disjoint(int n) {
|
||||
father = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
father[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
public void join(int n, int m) {
|
||||
n = find(n);
|
||||
m = find(m);
|
||||
if (n == m) return;
|
||||
father[n] = m;
|
||||
}
|
||||
|
||||
public int find(int n) {
|
||||
return father[n] == n ? n : (father[n] = find(father[n]));
|
||||
}
|
||||
|
||||
public boolean isSame(int n, int m) {
|
||||
return find(n) == find(m);
|
||||
}
|
||||
}
|
||||
|
||||
// 删一条边之后判断是不是树
|
||||
public static boolean isTreeAfterRemoveEdge(List<int[]> edges, int deleteEdge) {
|
||||
init(); // 初始化并查集
|
||||
static class Edge {
|
||||
int s;
|
||||
int t;
|
||||
|
||||
public Edge(int s, int t) {
|
||||
this.s = s;
|
||||
this.t = t;
|
||||
}
|
||||
}
|
||||
|
||||
static class Node {
|
||||
int id;
|
||||
int in;
|
||||
int out;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
List<Edge> edges = new ArrayList<>();
|
||||
Node[] nodeMap = new Node[n + 1];
|
||||
for (int i = 1; i <= n; i++) {
|
||||
nodeMap[i] = new Node();
|
||||
}
|
||||
Integer doubleIn = null;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (i == deleteEdge) continue;
|
||||
if (same(edges.get(i)[0], edges.get(i)[1])) { // 如果构成有向环了,一定不是树
|
||||
int s = scanner.nextInt();
|
||||
int t = scanner.nextInt();
|
||||
//记录入度
|
||||
nodeMap[t].in++;
|
||||
if (!(nodeMap[t].in < 2)) doubleIn = t;
|
||||
Edge edge = new Edge(s, t);
|
||||
edges.add(edge);
|
||||
}
|
||||
Edge result = null;
|
||||
//存在入度为2的节点,既要消除入度为2的问题同时解除可能存在的环
|
||||
if (doubleIn != null) {
|
||||
List<Edge> doubleInEdges = new ArrayList<>();
|
||||
for (Edge edge : edges) {
|
||||
if (edge.t == doubleIn) doubleInEdges.add(edge);
|
||||
if (doubleInEdges.size() == 2) break;
|
||||
}
|
||||
Edge edge = doubleInEdges.get(1);
|
||||
if (isTreeWithExclude(edges, edge, nodeMap)) {
|
||||
result = edge;
|
||||
} else {
|
||||
result = doubleInEdges.get(0);
|
||||
}
|
||||
} else {
|
||||
//不存在入度为2的节点,则只需要解除环即可
|
||||
result = getRemoveEdge(edges, nodeMap);
|
||||
}
|
||||
|
||||
System.out.println(result.s + " " + result.t);
|
||||
}
|
||||
|
||||
public static boolean isTreeWithExclude(List<Edge> edges, Edge exculdEdge, Node[] nodeMap) {
|
||||
Disjoint disjoint = new Disjoint(nodeMap.length + 1);
|
||||
for (Edge edge : edges) {
|
||||
if (edge == exculdEdge) continue;
|
||||
//成环则不是树
|
||||
if (disjoint.isSame(edge.s, edge.t)) {
|
||||
return false;
|
||||
}
|
||||
join(edges.get(i)[0], edges.get(i)[1]);
|
||||
disjoint.join(edge.s, edge.t);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
List<int[]> edges = new ArrayList<>(); // 存储所有的边
|
||||
public static Edge getRemoveEdge(List<Edge> edges, Node[] nodeMap) {
|
||||
int length = nodeMap.length;
|
||||
Disjoint disjoint = new Disjoint(length);
|
||||
|
||||
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}); // 将边加入列表
|
||||
for (Edge edge : edges) {
|
||||
if (disjoint.isSame(edge.s, edge.t)) return edge;
|
||||
disjoint.join(edge.s, edge.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);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Python
|
||||
|
||||
```python
|
||||
|
Reference in New Issue
Block a user