-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
516b76a
commit eaa4fb9
Showing
8 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,4 +69,8 @@ add_executable(algs4 | |
PrimMST.cpp | ||
PrimMST.h | ||
tests/testGraph.cpp | ||
UF.cpp | ||
UF.h | ||
tests/testUF.h | ||
tests/testUF.cpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "UF.h" | ||
|
||
UF::UF(int N) : id(N), sz(N), count_(N) { | ||
for (int i = 0; i < N; i++) { | ||
id[i] = i; | ||
sz[i] = 1; | ||
} | ||
} | ||
|
||
int UF::find(int p) const { | ||
while (p != id[p]) p = id[p]; | ||
return p; | ||
} | ||
|
||
void UF::unionize(int p, int q) { | ||
int i = find(p); | ||
int j = find(q); | ||
if (i == j) return; | ||
if (sz[i] < sz[j]) { | ||
id[i] = j; | ||
sz[j] += sz[i]; | ||
} else { | ||
id[j] = i; | ||
sz[i] += sz[j]; | ||
} | ||
--count_; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef UF_H | ||
#define UF_H | ||
#include <vector> | ||
|
||
|
||
class UF { | ||
private: | ||
std::vector<int> id; // 父链接数组(以触点为索引) | ||
std::vector<int> sz; // 各个根节点对应的分量的大小(以触点为索引) | ||
int count_; // 分量数量 | ||
|
||
public: | ||
UF(int N); | ||
|
||
int count() const { return count_; } | ||
|
||
bool connected(int p, int q) const { return find(p) == find(q); } | ||
|
||
int find(int p) const; | ||
|
||
void unionize(int p, int q); | ||
}; | ||
|
||
|
||
#endif //UF_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
10 | ||
4 3 | ||
3 8 | ||
6 5 | ||
9 4 | ||
2 1 | ||
8 9 | ||
5 0 | ||
7 2 | ||
6 1 | ||
1 0 | ||
6 7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "testUF.h" | ||
#include "UF.h" | ||
#include <iostream> | ||
|
||
void testUF() { | ||
UF uf([]() { | ||
int N; | ||
return std::cin >> N, N; | ||
}()); | ||
for (int p, q; std::cin >> p >> q;) { | ||
if (uf.connected(p, q)) continue; | ||
uf.unionize(p, q); | ||
std::cout << p << " " << q << "\n"; | ||
} | ||
std::cout << uf.count() << " components" << "\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef TESTUF_H | ||
#define TESTUF_H | ||
|
||
void testUF(); | ||
|
||
#endif //TESTUF_H |