Summary: Added an iterative implementation of the find function in the union-find template to prevent stack overflow problems caused by recursive calls.

This commit is contained in:
wbingb
2024-04-11 22:37:08 +08:00
parent f5fcd6273c
commit d811a8ee78
6 changed files with 222 additions and 3 deletions

1
.obsidian/app.json vendored Normal file
View File

@ -0,0 +1 @@
{}

3
.obsidian/appearance.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"accentColor": ""
}

30
.obsidian/core-plugins-migration.json vendored Normal file
View File

@ -0,0 +1,30 @@
{
"file-explorer": true,
"global-search": true,
"switcher": true,
"graph": true,
"backlink": true,
"canvas": true,
"outgoing-link": true,
"tag-pane": true,
"properties": false,
"page-preview": true,
"daily-notes": true,
"templates": true,
"note-composer": true,
"command-palette": true,
"slash-command": false,
"editor-status": true,
"bookmarks": true,
"markdown-importer": false,
"zk-prefixer": false,
"random-note": false,
"outline": true,
"word-count": true,
"slides": false,
"audio-recorder": false,
"workspaces": false,
"file-recovery": true,
"publish": false,
"sync": false
}

20
.obsidian/core-plugins.json vendored Normal file
View File

@ -0,0 +1,20 @@
[
"file-explorer",
"global-search",
"switcher",
"graph",
"backlink",
"canvas",
"outgoing-link",
"tag-pane",
"page-preview",
"daily-notes",
"templates",
"note-composer",
"command-palette",
"editor-status",
"bookmarks",
"outline",
"word-count",
"file-recovery"
]

147
.obsidian/workspace.json vendored Normal file
View File

@ -0,0 +1,147 @@
{
"main": {
"id": "14608cf8b641f651",
"type": "split",
"children": [
{
"id": "7b559c19d2418ebd",
"type": "tabs",
"children": [
{
"id": "a006865600bc4e20",
"type": "leaf",
"state": {
"type": "empty",
"state": {}
}
}
]
}
],
"direction": "vertical"
},
"left": {
"id": "340e6a79c670a47b",
"type": "split",
"children": [
{
"id": "c5f81ca398c18cda",
"type": "tabs",
"children": [
{
"id": "51f9f8f44ecceb89",
"type": "leaf",
"state": {
"type": "file-explorer",
"state": {
"sortOrder": "alphabetical"
}
}
},
{
"id": "0cff567244d7cff5",
"type": "leaf",
"state": {
"type": "search",
"state": {
"query": "1971",
"matchingCase": false,
"explainSearch": false,
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical"
}
}
},
{
"id": "209574c920774a4d",
"type": "leaf",
"state": {
"type": "bookmarks",
"state": {}
}
}
],
"currentTab": 1
}
],
"direction": "horizontal",
"width": 300
},
"right": {
"id": "93ce8f4c11893983",
"type": "split",
"children": [
{
"id": "da8cbb56e5ee7c52",
"type": "tabs",
"children": [
{
"id": "dfccbc1ebc0bb831",
"type": "leaf",
"state": {
"type": "backlink",
"state": {
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
"showSearch": false,
"searchQuery": "",
"backlinkCollapsed": false,
"unlinkedCollapsed": true
}
}
},
{
"id": "9d2201419a111fe4",
"type": "leaf",
"state": {
"type": "outgoing-link",
"state": {
"linksCollapsed": false,
"unlinkedCollapsed": true
}
}
},
{
"id": "dc86ccf1b01bc02c",
"type": "leaf",
"state": {
"type": "tag",
"state": {
"sortOrder": "frequency",
"useHierarchy": true
}
}
},
{
"id": "ec512545d2a7f2e5",
"type": "leaf",
"state": {
"type": "outline",
"state": {}
}
}
]
}
],
"direction": "horizontal",
"width": 300,
"collapsed": true
},
"left-ribbon": {
"hiddenItems": {
"switcher:打开快速切换": false,
"graph:查看关系图谱": false,
"canvas:新建白板": false,
"daily-notes:打开/创建今天的日记": false,
"templates:插入模板": false,
"command-palette:打开命令面板": false
}
},
"active": "a006865600bc4e20",
"lastOpenFiles": [
"problems/1971.寻找图中是否存在路径.md",
"README.md"
]
}

View File

@ -47,11 +47,22 @@ void init() {
father[i] = i;
}
}
// 并查集里寻根的过程
// 并查集里寻根的过程,这里递归调用当题目数据过多,递归调用可能会发生栈溢出
int find(int u) {
return u == father[u] ? u : father[u] = find(father[u]); // 路径压缩
}
// 使用迭代的方法可以避免栈溢出问题
int find(int x) {
while (x != parent[x]) {
// 路径压缩直接将x链接到其祖先节点减少树的高度
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
}
// 判断 u 和 v是否找到同一个根
bool isSame(int u, int v) {
u = find(u);
@ -84,6 +95,8 @@ void join(int u, int v) {
此时我们就可以直接套用并查集模板。
本题在join函数调用find函数时如果是递归调用会发生栈溢出提示建议使用迭代方法
使用 join(int u, int v)将每条边加入到并查集。
最后 isSame(int u, int v) 判断是否是同一个根 就可以了。
@ -102,8 +115,13 @@ private:
}
}
// 并查集里寻根的过程
int find(int u) {
return u == father[u] ? u : father[u] = find(father[u]);
int find(int x) {
while (x != parent[x]) {
// 路径压缩直接将x链接到其祖先节点减少树的高度
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
}
// 判断 u 和 v是否找到同一个根