-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuickFind.cs
More file actions
39 lines (33 loc) · 876 Bytes
/
Copy pathQuickFind.cs
File metadata and controls
39 lines (33 loc) · 876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Linq;
namespace DataStructure.Set
{
public class QuickFind : UnionFindBase
{
public QuickFind(int length) : base(length){ }
/// O(1)
public override int Find(int p)
{
Validate(p);
return Connections[p];
}
/// O(1)
public override bool IsConnected(int p, int q)
{
Validate(p);
Validate(q);
return Connections[q] == Connections[p];
}
/// O(n)
public override void Connect(int p, int q)
{
var pId = Connections[p];
var qId = Connections[q];
if (pId == qId) return;
for (var i = 0; i < Connections.Length; i++)
{
if (Connections[i] == pId) { Connections[i] = qId; }
}
Count--;
}
}
}