博客 https://blog.lenyiin.com/union-find/ 的代码仓库
This commit is contained in:
commit
30110bf90d
5
Linux/Makefile
Normal file
5
Linux/Makefile
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
unionfindset:UnionFindSet.cc
|
||||||
|
g++ -o $@ $^
|
||||||
|
.PHONY:clean
|
||||||
|
clean:
|
||||||
|
rm -f unionfindset
|
42
Linux/UnionFindSet.cc
Normal file
42
Linux/UnionFindSet.cc
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#include "UnionFindSet.hpp"
|
||||||
|
|
||||||
|
void TestUnionFindSet_1()
|
||||||
|
{
|
||||||
|
Lenyiin::UnionFindSet ufs(10);
|
||||||
|
ufs.Union(8, 9);
|
||||||
|
ufs.Union(7, 8);
|
||||||
|
ufs.Union(6, 7);
|
||||||
|
ufs.Union(5, 6);
|
||||||
|
ufs.Union(4, 5);
|
||||||
|
ufs.Union(3, 4);
|
||||||
|
ufs.Union(2, 3);
|
||||||
|
ufs.Union(1, 2);
|
||||||
|
ufs.Union(0, 1);
|
||||||
|
ufs.Print();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestUnionFindSet_2()
|
||||||
|
{
|
||||||
|
Lenyiin::UnionFindSet ufs(10);
|
||||||
|
ufs.Union(0, 1);
|
||||||
|
ufs.Union(2, 3);
|
||||||
|
ufs.Union(4, 5);
|
||||||
|
ufs.Union(6, 7);
|
||||||
|
ufs.Union(8, 9);
|
||||||
|
ufs.Union(1, 3);
|
||||||
|
ufs.Union(5, 7);
|
||||||
|
ufs.Union(0, 2);
|
||||||
|
ufs.Union(4, 6);
|
||||||
|
ufs.Union(0, 4);
|
||||||
|
|
||||||
|
ufs.FindRoot(0);
|
||||||
|
ufs.Print();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
TestUnionFindSet_1();
|
||||||
|
TestUnionFindSet_2();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
120
Linux/UnionFindSet.hpp
Normal file
120
Linux/UnionFindSet.hpp
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
namespace Lenyiin
|
||||||
|
{
|
||||||
|
class UnionFindSet
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// 初始化: 为每个元素单独分配一个集合, 初始时每个集合大小为 1, 用 -1 表示
|
||||||
|
UnionFindSet(int n)
|
||||||
|
: _v(n, -1) // 初始化所有元素都为根节点, 且集合大小为1
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查找操作: 找到元素 x 所属集合的根节点, 并使用路径压缩优化
|
||||||
|
int FindRoot(int x)
|
||||||
|
{
|
||||||
|
int root = x;
|
||||||
|
// 查找根节点, 沿着父节点链向上寻找
|
||||||
|
while (_v[root] >= 0)
|
||||||
|
{
|
||||||
|
root = _v[root];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 路径压缩: 将路径上所有节点直接挂在根节点下
|
||||||
|
while (x != root)
|
||||||
|
{
|
||||||
|
int parent = _v[x]; // 保存当前节点的父节点
|
||||||
|
_v[x] = root; // 将当前节点直接连接到根节点
|
||||||
|
x = parent; // 继续处理父节点
|
||||||
|
}
|
||||||
|
|
||||||
|
return root; // 返回根节点
|
||||||
|
}
|
||||||
|
|
||||||
|
// 合并两个集合: 使用按秩合并优化
|
||||||
|
bool Union(int x1, int x2)
|
||||||
|
{
|
||||||
|
int root1 = FindRoot(x1);
|
||||||
|
int root2 = FindRoot(x2);
|
||||||
|
|
||||||
|
// 如果本身就在一个集合里, 则无需合并
|
||||||
|
if (root1 == root2)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按秩合并: 始终将较小的集合挂到较大的集合上
|
||||||
|
if (_v[root1] < _v[root2]) // root1 集合规模较大(负值越小, 集合越大)
|
||||||
|
{
|
||||||
|
_v[root1] += _v[root2]; // 增加 root1 集合的大小
|
||||||
|
_v[root2] = root1; // root2 挂到 root1 上
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_v[root2] += _v[root1]; // 增加 root2 集合的大小
|
||||||
|
_v[root1] = root2; // root1 挂到 root2 上
|
||||||
|
}
|
||||||
|
|
||||||
|
return true; // 合并成功
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断两个元素是否在同一个集合中
|
||||||
|
bool InSet(int x1, int x2)
|
||||||
|
{
|
||||||
|
return FindRoot(x2) == FindRoot(x2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 统计当前集合的个数
|
||||||
|
size_t SetSize()
|
||||||
|
{
|
||||||
|
size_t count = 0;
|
||||||
|
for (auto e : _v)
|
||||||
|
{
|
||||||
|
if (e < 0) // 只有根节点存储负值, 表示集合的大小
|
||||||
|
{
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印当前并查集的结构 (用于调试)
|
||||||
|
void Print() const
|
||||||
|
{
|
||||||
|
for (int i = 0; i < _v.size(); ++i)
|
||||||
|
{
|
||||||
|
printf("v[%d] == %d\n", i, _v[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<int> _v; // 存储每个元素的父节点或集合大小
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// namespace Lenyiin
|
||||||
|
//{
|
||||||
|
// template <class T>
|
||||||
|
// class UnionFindSet
|
||||||
|
// {
|
||||||
|
// public:
|
||||||
|
// UnionFindSet(const T* a, size_t n)
|
||||||
|
// {
|
||||||
|
// for (size_t i = 0; i < n; ++i)
|
||||||
|
// {
|
||||||
|
// _v.push_back(a[i]);
|
||||||
|
// _indexmap[a[i]] = i;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private:
|
||||||
|
// vector<T> _v; // 人找编号
|
||||||
|
// map<T, int> _indexmap; // 编号找人
|
||||||
|
// };
|
||||||
|
// }
|
BIN
Linux/unionfindset
Normal file
BIN
Linux/unionfindset
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Windows_UnionFindSet/.vs/Windows_UnionFindSet/v17/.suo
Normal file
BIN
Windows_UnionFindSet/.vs/Windows_UnionFindSet/v17/.suo
Normal file
Binary file not shown.
BIN
Windows_UnionFindSet/.vs/Windows_UnionFindSet/v17/Browse.VC.db
Normal file
BIN
Windows_UnionFindSet/.vs/Windows_UnionFindSet/v17/Browse.VC.db
Normal file
Binary file not shown.
@ -0,0 +1,66 @@
|
|||||||
|
{
|
||||||
|
"Version": 1,
|
||||||
|
"WorkspaceRootPath": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\",
|
||||||
|
"Documents": [
|
||||||
|
{
|
||||||
|
"AbsoluteMoniker": "D:0:0:{E780F4E3-14DF-4F84-A802-08BD1E4086CC}|Windows_UnionFindSet.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\UnionFindSet.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||||
|
"RelativeMoniker": "D:0:0:{E780F4E3-14DF-4F84-A802-08BD1E4086CC}|Windows_UnionFindSet.vcxproj|solutionrelative:UnionFindSet.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"AbsoluteMoniker": "D:0:0:{E780F4E3-14DF-4F84-A802-08BD1E4086CC}|Windows_UnionFindSet.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\UnionFindSet.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||||
|
"RelativeMoniker": "D:0:0:{E780F4E3-14DF-4F84-A802-08BD1E4086CC}|Windows_UnionFindSet.vcxproj|solutionrelative:UnionFindSet.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"DocumentGroupContainers": [
|
||||||
|
{
|
||||||
|
"Orientation": 0,
|
||||||
|
"VerticalTabListWidth": 256,
|
||||||
|
"DocumentGroups": [
|
||||||
|
{
|
||||||
|
"DockedWidth": 267,
|
||||||
|
"SelectedChildIndex": 3,
|
||||||
|
"Children": [
|
||||||
|
{
|
||||||
|
"$type": "Bookmark",
|
||||||
|
"Name": "ST:128:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$type": "Bookmark",
|
||||||
|
"Name": "ST:129:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$type": "Bookmark",
|
||||||
|
"Name": "ST:128:0:{1fc202d4-d401-403c-9834-5b218574bb67}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$type": "Document",
|
||||||
|
"DocumentIndex": 0,
|
||||||
|
"Title": "UnionFindSet.hpp",
|
||||||
|
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\UnionFindSet.hpp",
|
||||||
|
"RelativeDocumentMoniker": "UnionFindSet.hpp",
|
||||||
|
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\UnionFindSet.hpp*",
|
||||||
|
"RelativeToolTip": "UnionFindSet.hpp*",
|
||||||
|
"ViewState": "AgIAAFEAAAAAAAAAAAAowG0AAAAGAAAAAAAAAA==",
|
||||||
|
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
|
||||||
|
"WhenOpened": "2024-10-10T16:56:48.361Z",
|
||||||
|
"EditorCaption": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$type": "Document",
|
||||||
|
"DocumentIndex": 1,
|
||||||
|
"Title": "UnionFindSet.cpp",
|
||||||
|
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\UnionFindSet.cpp",
|
||||||
|
"RelativeDocumentMoniker": "UnionFindSet.cpp",
|
||||||
|
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\UnionFindSet.cpp*",
|
||||||
|
"RelativeToolTip": "UnionFindSet.cpp*",
|
||||||
|
"ViewState": "AgIAAAAAAAAAAAAAAAAowCkAAAABAAAAAAAAAA==",
|
||||||
|
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
|
||||||
|
"WhenOpened": "2024-10-10T16:56:29.902Z",
|
||||||
|
"EditorCaption": ""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
{
|
||||||
|
"Version": 1,
|
||||||
|
"WorkspaceRootPath": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\",
|
||||||
|
"Documents": [
|
||||||
|
{
|
||||||
|
"AbsoluteMoniker": "D:0:0:{E780F4E3-14DF-4F84-A802-08BD1E4086CC}|Windows_UnionFindSet.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\UnionFindSet.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||||
|
"RelativeMoniker": "D:0:0:{E780F4E3-14DF-4F84-A802-08BD1E4086CC}|Windows_UnionFindSet.vcxproj|solutionrelative:UnionFindSet.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"AbsoluteMoniker": "D:0:0:{E780F4E3-14DF-4F84-A802-08BD1E4086CC}|Windows_UnionFindSet.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\UnionFindSet.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
|
||||||
|
"RelativeMoniker": "D:0:0:{E780F4E3-14DF-4F84-A802-08BD1E4086CC}|Windows_UnionFindSet.vcxproj|solutionrelative:UnionFindSet.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"DocumentGroupContainers": [
|
||||||
|
{
|
||||||
|
"Orientation": 0,
|
||||||
|
"VerticalTabListWidth": 256,
|
||||||
|
"DocumentGroups": [
|
||||||
|
{
|
||||||
|
"DockedWidth": 267,
|
||||||
|
"SelectedChildIndex": 3,
|
||||||
|
"Children": [
|
||||||
|
{
|
||||||
|
"$type": "Bookmark",
|
||||||
|
"Name": "ST:128:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$type": "Bookmark",
|
||||||
|
"Name": "ST:129:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$type": "Bookmark",
|
||||||
|
"Name": "ST:128:0:{1fc202d4-d401-403c-9834-5b218574bb67}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$type": "Document",
|
||||||
|
"DocumentIndex": 0,
|
||||||
|
"Title": "UnionFindSet.hpp",
|
||||||
|
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\UnionFindSet.hpp",
|
||||||
|
"RelativeDocumentMoniker": "UnionFindSet.hpp",
|
||||||
|
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\UnionFindSet.hpp",
|
||||||
|
"RelativeToolTip": "UnionFindSet.hpp",
|
||||||
|
"ViewState": "AgIAAFEAAAAAAAAAAAAowF4AAAAAAAAAAAAAAA==",
|
||||||
|
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
|
||||||
|
"WhenOpened": "2024-10-10T16:56:48.361Z",
|
||||||
|
"EditorCaption": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$type": "Document",
|
||||||
|
"DocumentIndex": 1,
|
||||||
|
"Title": "UnionFindSet.cpp",
|
||||||
|
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\UnionFindSet.cpp",
|
||||||
|
"RelativeDocumentMoniker": "UnionFindSet.cpp",
|
||||||
|
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\12_UnionFind\\Windows_UnionFindSet\\UnionFindSet.cpp",
|
||||||
|
"RelativeToolTip": "UnionFindSet.cpp",
|
||||||
|
"ViewState": "AgIAAAAAAAAAAAAAAAAowCkAAAABAAAAAAAAAA==",
|
||||||
|
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
|
||||||
|
"WhenOpened": "2024-10-10T16:56:29.902Z",
|
||||||
|
"EditorCaption": ""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Binary file not shown.
42
Windows_UnionFindSet/UnionFindSet.cpp
Normal file
42
Windows_UnionFindSet/UnionFindSet.cpp
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#include "UnionFindSet.hpp"
|
||||||
|
|
||||||
|
void TestUnionFindSet_1()
|
||||||
|
{
|
||||||
|
Lenyiin::UnionFindSet ufs(10);
|
||||||
|
ufs.Union(8, 9);
|
||||||
|
ufs.Union(7, 8);
|
||||||
|
ufs.Union(6, 7);
|
||||||
|
ufs.Union(5, 6);
|
||||||
|
ufs.Union(4, 5);
|
||||||
|
ufs.Union(3, 4);
|
||||||
|
ufs.Union(2, 3);
|
||||||
|
ufs.Union(1, 2);
|
||||||
|
ufs.Union(0, 1);
|
||||||
|
ufs.Print();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestUnionFindSet_2()
|
||||||
|
{
|
||||||
|
Lenyiin::UnionFindSet ufs(10);
|
||||||
|
ufs.Union(0, 1);
|
||||||
|
ufs.Union(2, 3);
|
||||||
|
ufs.Union(4, 5);
|
||||||
|
ufs.Union(6, 7);
|
||||||
|
ufs.Union(8, 9);
|
||||||
|
ufs.Union(1, 3);
|
||||||
|
ufs.Union(5, 7);
|
||||||
|
ufs.Union(0, 2);
|
||||||
|
ufs.Union(4, 6);
|
||||||
|
ufs.Union(0, 4);
|
||||||
|
|
||||||
|
ufs.FindRoot(0);
|
||||||
|
ufs.Print();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
TestUnionFindSet_1();
|
||||||
|
TestUnionFindSet_2();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
120
Windows_UnionFindSet/UnionFindSet.hpp
Normal file
120
Windows_UnionFindSet/UnionFindSet.hpp
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
namespace Lenyiin
|
||||||
|
{
|
||||||
|
class UnionFindSet
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// 初始化: 为每个元素单独分配一个集合, 初始时每个集合大小为 1, 用 -1 表示
|
||||||
|
UnionFindSet(int n)
|
||||||
|
: _v(n, -1) // 初始化所有元素都为根节点, 且集合大小为1
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查找操作: 找到元素 x 所属集合的根节点, 并使用路径压缩优化
|
||||||
|
int FindRoot(int x)
|
||||||
|
{
|
||||||
|
int root = x;
|
||||||
|
// 查找根节点, 沿着父节点链向上寻找
|
||||||
|
while (_v[root] >= 0)
|
||||||
|
{
|
||||||
|
root = _v[root];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 路径压缩: 将路径上所有节点直接挂在根节点下
|
||||||
|
while (x != root)
|
||||||
|
{
|
||||||
|
int parent = _v[x]; // 保存当前节点的父节点
|
||||||
|
_v[x] = root; // 将当前节点直接连接到根节点
|
||||||
|
x = parent; // 继续处理父节点
|
||||||
|
}
|
||||||
|
|
||||||
|
return root; // 返回根节点
|
||||||
|
}
|
||||||
|
|
||||||
|
// 合并两个集合: 使用按秩合并优化
|
||||||
|
bool Union(int x1, int x2)
|
||||||
|
{
|
||||||
|
int root1 = FindRoot(x1);
|
||||||
|
int root2 = FindRoot(x2);
|
||||||
|
|
||||||
|
// 如果本身就在一个集合里, 则无需合并
|
||||||
|
if (root1 == root2)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按秩合并: 始终将较小的集合挂到较大的集合上
|
||||||
|
if (_v[root1] < _v[root2]) // root1 集合规模较大(负值越小, 集合越大)
|
||||||
|
{
|
||||||
|
_v[root1] += _v[root2]; // 增加 root1 集合的大小
|
||||||
|
_v[root2] = root1; // root2 挂到 root1 上
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_v[root2] += _v[root1]; // 增加 root2 集合的大小
|
||||||
|
_v[root1] = root2; // root1 挂到 root2 上
|
||||||
|
}
|
||||||
|
|
||||||
|
return true; // 合并成功
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断两个元素是否在同一个集合中
|
||||||
|
bool InSet(int x1, int x2)
|
||||||
|
{
|
||||||
|
return FindRoot(x2) == FindRoot(x2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 统计当前集合的个数
|
||||||
|
size_t SetSize()
|
||||||
|
{
|
||||||
|
size_t count = 0;
|
||||||
|
for (auto e : _v)
|
||||||
|
{
|
||||||
|
if (e < 0) // 只有根节点存储负值, 表示集合的大小
|
||||||
|
{
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印当前并查集的结构 (用于调试)
|
||||||
|
void Print() const
|
||||||
|
{
|
||||||
|
for (int i = 0; i < _v.size(); ++i)
|
||||||
|
{
|
||||||
|
printf("v[%d] == %d\n", i, _v[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<int> _v; // 存储每个元素的父节点或集合大小
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// namespace Lenyiin
|
||||||
|
//{
|
||||||
|
// template <class T>
|
||||||
|
// class UnionFindSet
|
||||||
|
// {
|
||||||
|
// public:
|
||||||
|
// UnionFindSet(const T* a, size_t n)
|
||||||
|
// {
|
||||||
|
// for (size_t i = 0; i < n; ++i)
|
||||||
|
// {
|
||||||
|
// _v.push_back(a[i]);
|
||||||
|
// _indexmap[a[i]] = i;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private:
|
||||||
|
// vector<T> _v; // 人找编号
|
||||||
|
// map<T, int> _indexmap; // 编号找人
|
||||||
|
// };
|
||||||
|
// }
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1 @@
|
|||||||
|
E:\Git 仓库\公开仓库\12_UnionFind\Windows_UnionFindSet\UnionFindSet.cpp;E:\Git 仓库\公开仓库\12_UnionFind\Windows_UnionFindSet\Windows_.e780f4e3\x64\Debug\UnionFindSet.obj
|
@ -0,0 +1,2 @@
|
|||||||
|
PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.41.34120:TargetPlatformVersion=10.0.22000.0:
|
||||||
|
Debug|x64|E:\Git 仓库\公开仓库\12_UnionFind\Windows_UnionFindSet\|
|
Binary file not shown.
Binary file not shown.
@ -0,0 +1,2 @@
|
|||||||
|
^E:\GIT 仓库\公开仓库\12_UNIONFIND\WINDOWS_UNIONFINDSET\WINDOWS_.E780F4E3\X64\DEBUG\UNIONFINDSET.OBJ
|
||||||
|
E:\Git 仓库\公开仓库\12_UnionFind\Windows_UnionFindSet\Windows_.e780f4e3\x64\Debug\Windows_UnionFindSet.ilk
|
Binary file not shown.
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project>
|
||||||
|
<ProjectOutputs>
|
||||||
|
<ProjectOutput>
|
||||||
|
<FullPath>E:\Git 仓库\公开仓库\12_UnionFind\Windows_UnionFindSet\x64\Debug\Windows_UnionFindSet.exe</FullPath>
|
||||||
|
</ProjectOutput>
|
||||||
|
</ProjectOutputs>
|
||||||
|
<ContentFiles />
|
||||||
|
<SatelliteDlls />
|
||||||
|
<NonRecipeFileRefs />
|
||||||
|
</Project>
|
Binary file not shown.
@ -0,0 +1,2 @@
|
|||||||
|
UnionFindSet.cpp
|
||||||
|
Windows_UnionFindSet.vcxproj -> E:\Git 仓库\公开仓库\12_UnionFind\Windows_UnionFindSet\x64\Debug\Windows_UnionFindSet.exe
|
BIN
Windows_UnionFindSet/Windows_.e780f4e3/x64/Debug/vc143.idb
Normal file
BIN
Windows_UnionFindSet/Windows_.e780f4e3/x64/Debug/vc143.idb
Normal file
Binary file not shown.
BIN
Windows_UnionFindSet/Windows_.e780f4e3/x64/Debug/vc143.pdb
Normal file
BIN
Windows_UnionFindSet/Windows_.e780f4e3/x64/Debug/vc143.pdb
Normal file
Binary file not shown.
31
Windows_UnionFindSet/Windows_UnionFindSet.sln
Normal file
31
Windows_UnionFindSet/Windows_UnionFindSet.sln
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.11.35312.102
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Windows_UnionFindSet", "Windows_UnionFindSet.vcxproj", "{E780F4E3-14DF-4F84-A802-08BD1E4086CC}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|x64 = Debug|x64
|
||||||
|
Debug|x86 = Debug|x86
|
||||||
|
Release|x64 = Release|x64
|
||||||
|
Release|x86 = Release|x86
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{E780F4E3-14DF-4F84-A802-08BD1E4086CC}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{E780F4E3-14DF-4F84-A802-08BD1E4086CC}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{E780F4E3-14DF-4F84-A802-08BD1E4086CC}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
|
{E780F4E3-14DF-4F84-A802-08BD1E4086CC}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{E780F4E3-14DF-4F84-A802-08BD1E4086CC}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{E780F4E3-14DF-4F84-A802-08BD1E4086CC}.Release|x64.Build.0 = Release|x64
|
||||||
|
{E780F4E3-14DF-4F84-A802-08BD1E4086CC}.Release|x86.ActiveCfg = Release|Win32
|
||||||
|
{E780F4E3-14DF-4F84-A802-08BD1E4086CC}.Release|x86.Build.0 = Release|Win32
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {45BCDB8D-5567-4827-9713-EDD43FA81124}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
138
Windows_UnionFindSet/Windows_UnionFindSet.vcxproj
Normal file
138
Windows_UnionFindSet/Windows_UnionFindSet.vcxproj
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup Label="ProjectConfigurations">
|
||||||
|
<ProjectConfiguration Include="Debug|Win32">
|
||||||
|
<Configuration>Debug</Configuration>
|
||||||
|
<Platform>Win32</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Release|Win32">
|
||||||
|
<Configuration>Release</Configuration>
|
||||||
|
<Platform>Win32</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Debug|x64">
|
||||||
|
<Configuration>Debug</Configuration>
|
||||||
|
<Platform>x64</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Release|x64">
|
||||||
|
<Configuration>Release</Configuration>
|
||||||
|
<Platform>x64</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
</ItemGroup>
|
||||||
|
<PropertyGroup Label="Globals">
|
||||||
|
<VCProjectVersion>17.0</VCProjectVersion>
|
||||||
|
<Keyword>Win32Proj</Keyword>
|
||||||
|
<ProjectGuid>{e780f4e3-14df-4f84-a802-08bd1e4086cc}</ProjectGuid>
|
||||||
|
<RootNamespace>WindowsUnionFindSet</RootNamespace>
|
||||||
|
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v143</PlatformToolset>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v143</PlatformToolset>
|
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v143</PlatformToolset>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v143</PlatformToolset>
|
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||||
|
<ImportGroup Label="ExtensionSettings">
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="Shared">
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<PropertyGroup Label="UserMacros" />
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="UnionFindSet.cpp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="UnionFindSet.hpp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
27
Windows_UnionFindSet/Windows_UnionFindSet.vcxproj.filters
Normal file
27
Windows_UnionFindSet/Windows_UnionFindSet.vcxproj.filters
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup>
|
||||||
|
<Filter Include="源文件">
|
||||||
|
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||||
|
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="头文件">
|
||||||
|
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||||
|
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="资源文件">
|
||||||
|
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||||
|
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||||
|
</Filter>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="UnionFindSet.cpp">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="UnionFindSet.hpp">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
4
Windows_UnionFindSet/Windows_UnionFindSet.vcxproj.user
Normal file
4
Windows_UnionFindSet/Windows_UnionFindSet.vcxproj.user
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup />
|
||||||
|
</Project>
|
BIN
Windows_UnionFindSet/x64/Debug/Windows_UnionFindSet.exe
Normal file
BIN
Windows_UnionFindSet/x64/Debug/Windows_UnionFindSet.exe
Normal file
Binary file not shown.
BIN
Windows_UnionFindSet/x64/Debug/Windows_UnionFindSet.pdb
Normal file
BIN
Windows_UnionFindSet/x64/Debug/Windows_UnionFindSet.pdb
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user