Skip to content

Commit e307732

Browse files
authored
feat: update lc problems (doocs#3679)
1 parent 5ac2738 commit e307732

File tree

20 files changed

+1813
-399
lines changed

20 files changed

+1813
-399
lines changed

solution/0600-0699/0685.Redundant Connection II/README.md

Lines changed: 610 additions & 111 deletions
Large diffs are not rendered by default.

solution/0600-0699/0685.Redundant Connection II/README_EN.md

Lines changed: 613 additions & 110 deletions
Large diffs are not rendered by default.
Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,43 @@
1-
class UnionFind {
2-
public:
3-
vector<int> p;
4-
int n;
5-
6-
UnionFind(int _n)
7-
: n(_n)
8-
, p(_n) {
9-
iota(p.begin(), p.end(), 0);
10-
}
11-
12-
bool unite(int a, int b) {
13-
int pa = find(a), pb = find(b);
14-
if (pa == pb) return false;
15-
p[pa] = pb;
16-
--n;
17-
return true;
18-
}
19-
20-
int find(int x) {
21-
if (p[x] != x) p[x] = find(p[x]);
22-
return p[x];
23-
}
24-
};
25-
261
class Solution {
272
public:
283
vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
294
int n = edges.size();
30-
vector<int> p(n + 1);
31-
for (int i = 0; i <= n; ++i) p[i] = i;
32-
UnionFind uf(n + 1);
33-
int conflict = -1, cycle = -1;
5+
vector<int> ind(n);
6+
for (const auto& e : edges) {
7+
++ind[e[1] - 1];
8+
}
9+
vector<int> dup;
3410
for (int i = 0; i < n; ++i) {
35-
int u = edges[i][0], v = edges[i][1];
36-
if (p[v] != v)
37-
conflict = i;
38-
else {
39-
p[v] = u;
40-
if (!uf.unite(u, v)) cycle = i;
11+
if (ind[edges[i][1] - 1] == 2) {
12+
dup.push_back(i);
4113
}
4214
}
43-
if (conflict == -1) return edges[cycle];
44-
int v = edges[conflict][1];
45-
if (cycle != -1) return {p[v], v};
46-
return edges[conflict];
15+
vector<int> p(n);
16+
iota(p.begin(), p.end(), 0);
17+
function<int(int)> find = [&](int x) {
18+
return x == p[x] ? x : p[x] = find(p[x]);
19+
};
20+
if (!dup.empty()) {
21+
for (int i = 0; i < n; ++i) {
22+
if (i == dup[1]) {
23+
continue;
24+
}
25+
int pu = find(edges[i][0] - 1);
26+
int pv = find(edges[i][1] - 1);
27+
if (pu == pv) {
28+
return edges[dup[0]];
29+
}
30+
p[pu] = pv;
31+
}
32+
return edges[dup[1]];
33+
}
34+
for (int i = 0;; ++i) {
35+
int pu = find(edges[i][0] - 1);
36+
int pv = find(edges[i][1] - 1);
37+
if (pu == pv) {
38+
return edges[i];
39+
}
40+
p[pu] = pv;
41+
}
4742
}
48-
};
43+
};
Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,45 @@
1-
type unionFind struct {
2-
p []int
3-
n int
4-
}
5-
6-
func newUnionFind(n int) *unionFind {
7-
p := make([]int, n)
8-
for i := range p {
9-
p[i] = i
10-
}
11-
return &unionFind{p, n}
12-
}
13-
14-
func (uf *unionFind) find(x int) int {
15-
if uf.p[x] != x {
16-
uf.p[x] = uf.find(uf.p[x])
17-
}
18-
return uf.p[x]
19-
}
20-
21-
func (uf *unionFind) union(a, b int) bool {
22-
if uf.find(a) == uf.find(b) {
23-
return false
24-
}
25-
uf.p[uf.find(a)] = uf.find(b)
26-
uf.n--
27-
return true
28-
}
29-
301
func findRedundantDirectedConnection(edges [][]int) []int {
312
n := len(edges)
32-
p := make([]int, n+1)
3+
ind := make([]int, n)
4+
for _, e := range edges {
5+
ind[e[1]-1]++
6+
}
7+
dup := []int{}
8+
for i, e := range edges {
9+
if ind[e[1]-1] == 2 {
10+
dup = append(dup, i)
11+
}
12+
}
13+
p := make([]int, n)
3314
for i := range p {
3415
p[i] = i
3516
}
36-
uf := newUnionFind(n + 1)
37-
conflict, cycle := -1, -1
38-
for i, e := range edges {
39-
u, v := e[0], e[1]
40-
if p[v] != v {
41-
conflict = i
42-
} else {
43-
p[v] = u
44-
if !uf.union(u, v) {
45-
cycle = i
46-
}
17+
var find func(int) int
18+
find = func(x int) int {
19+
if p[x] != x {
20+
p[x] = find(p[x])
4721
}
22+
return p[x]
4823
}
49-
if conflict == -1 {
50-
return edges[cycle]
24+
if len(dup) > 0 {
25+
for i, e := range edges {
26+
if i == dup[1] {
27+
continue
28+
}
29+
pu, pv := find(e[0]-1), find(e[1]-1)
30+
if pu == pv {
31+
return edges[dup[0]]
32+
}
33+
p[pu] = pv
34+
}
35+
return edges[dup[1]]
5136
}
52-
v := edges[conflict][1]
53-
if cycle != -1 {
54-
return []int{p[v], v}
37+
for _, e := range edges {
38+
pu, pv := find(e[0]-1), find(e[1]-1)
39+
if pu == pv {
40+
return e
41+
}
42+
p[pu] = pv
5543
}
56-
return edges[conflict]
57-
}
44+
return nil
45+
}
Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,48 @@
11
class Solution {
2+
private int[] p;
3+
24
public int[] findRedundantDirectedConnection(int[][] edges) {
35
int n = edges.length;
4-
int[] p = new int[n + 1];
5-
for (int i = 0; i <= n; ++i) {
6-
p[i] = i;
7-
}
8-
UnionFind uf = new UnionFind(n + 1);
9-
int conflict = -1, cycle = -1;
10-
for (int i = 0; i < n; ++i) {
11-
int u = edges[i][0], v = edges[i][1];
12-
if (p[v] != v) {
13-
conflict = i;
14-
} else {
15-
p[v] = u;
16-
if (!uf.union(u, v)) {
17-
cycle = i;
18-
}
19-
}
6+
int[] ind = new int[n];
7+
for (var e : edges) {
8+
++ind[e[1] - 1];
209
}
21-
if (conflict == -1) {
22-
return edges[cycle];
23-
}
24-
int v = edges[conflict][1];
25-
if (cycle != -1) {
26-
return new int[] {p[v], v};
27-
}
28-
return edges[conflict];
29-
}
30-
}
31-
32-
class UnionFind {
33-
public int[] p;
34-
public int n;
35-
36-
public UnionFind(int n) {
10+
List<Integer> dup = new ArrayList<>();
3711
p = new int[n];
3812
for (int i = 0; i < n; ++i) {
13+
if (ind[edges[i][1] - 1] == 2) {
14+
dup.add(i);
15+
}
3916
p[i] = i;
4017
}
41-
this.n = n;
42-
}
43-
44-
public boolean union(int a, int b) {
45-
int pa = find(a);
46-
int pb = find(b);
47-
if (pa == pb) {
48-
return false;
18+
if (!dup.isEmpty()) {
19+
for (int i = 0; i < n; ++i) {
20+
if (i == dup.get(1)) {
21+
continue;
22+
}
23+
int pu = find(edges[i][0] - 1);
24+
int pv = find(edges[i][1] - 1);
25+
if (pu == pv) {
26+
return edges[dup.get(0)];
27+
}
28+
p[pu] = pv;
29+
}
30+
return edges[dup.get(1)];
31+
}
32+
for (int i = 0;; ++i) {
33+
int pu = find(edges[i][0] - 1);
34+
int pv = find(edges[i][1] - 1);
35+
if (pu == pv) {
36+
return edges[i];
37+
}
38+
p[pu] = pv;
4939
}
50-
p[pa] = pb;
51-
--n;
52-
return true;
5340
}
5441

55-
public int find(int x) {
42+
private int find(int x) {
5643
if (p[x] != x) {
5744
p[x] = find(p[x]);
5845
}
5946
return p[x];
6047
}
61-
}
48+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @param {number[][]} edges
3+
* @return {number[]}
4+
*/
5+
var findRedundantDirectedConnection = function (edges) {
6+
const n = edges.length;
7+
const ind = Array(n).fill(0);
8+
for (const [_, v] of edges) {
9+
++ind[v - 1];
10+
}
11+
const dup = [];
12+
for (let i = 0; i < n; ++i) {
13+
if (ind[edges[i][1] - 1] === 2) {
14+
dup.push(i);
15+
}
16+
}
17+
const p = Array.from({ length: n }, (_, i) => i);
18+
const find = x => {
19+
if (p[x] !== x) {
20+
p[x] = find(p[x]);
21+
}
22+
return p[x];
23+
};
24+
if (dup.length) {
25+
for (let i = 0; i < n; ++i) {
26+
if (i === dup[1]) {
27+
continue;
28+
}
29+
const [pu, pv] = [find(edges[i][0] - 1), find(edges[i][1] - 1)];
30+
if (pu === pv) {
31+
return edges[dup[0]];
32+
}
33+
p[pu] = pv;
34+
}
35+
return edges[dup[1]];
36+
}
37+
for (let i = 0; ; ++i) {
38+
const [pu, pv] = [find(edges[i][0] - 1), find(edges[i][1] - 1)];
39+
if (pu === pv) {
40+
return edges[i];
41+
}
42+
p[pu] = pv;
43+
}
44+
};
Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
1-
class UnionFind:
2-
def __init__(self, n):
3-
self.p = list(range(n))
4-
self.n = n
5-
6-
def union(self, a, b):
7-
if self.find(a) == self.find(b):
8-
return False
9-
self.p[self.find(a)] = self.find(b)
10-
self.n -= 1
11-
return True
12-
13-
def find(self, x):
14-
if self.p[x] != x:
15-
self.p[x] = self.find(self.p[x])
16-
return self.p[x]
17-
18-
191
class Solution:
202
def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]:
3+
def find(x: int) -> int:
4+
if p[x] != x:
5+
p[x] = find(p[x])
6+
return p[x]
7+
218
n = len(edges)
22-
p = list(range(n + 1))
23-
uf = UnionFind(n + 1)
24-
conflict = cycle = None
9+
ind = [0] * n
10+
for _, v in edges:
11+
ind[v - 1] += 1
12+
dup = [i for i, (_, v) in enumerate(edges) if ind[v - 1] == 2]
13+
p = list(range(n))
14+
if dup:
15+
for i, (u, v) in enumerate(edges):
16+
if i == dup[1]:
17+
continue
18+
pu, pv = find(u - 1), find(v - 1)
19+
if pu == pv:
20+
return edges[dup[0]]
21+
p[pu] = pv
22+
return edges[dup[1]]
2523
for i, (u, v) in enumerate(edges):
26-
if p[v] != v:
27-
conflict = i
28-
else:
29-
p[v] = u
30-
if not uf.union(u, v):
31-
cycle = i
32-
if conflict is None:
33-
return edges[cycle]
34-
v = edges[conflict][1]
35-
if cycle is not None:
36-
return [p[v], v]
37-
return edges[conflict]
24+
pu, pv = find(u - 1), find(v - 1)
25+
if pu == pv:
26+
return edges[i]
27+
p[pu] = pv

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy