From d811a8ee78f6ec724f17e98780a0dccba85498ed Mon Sep 17 00:00:00 2001 From: wbingb Date: Thu, 11 Apr 2024 22:37:08 +0800 Subject: [PATCH 1/2] Summary: Added an iterative implementation of the find function in the union-find template to prevent stack overflow problems caused by recursive calls. --- .obsidian/app.json | 1 + .obsidian/appearance.json | 3 + .obsidian/core-plugins-migration.json | 30 ++++ .obsidian/core-plugins.json | 20 +++ .obsidian/workspace.json | 147 ++++++++++++++++++ .../1971.寻找图中是否存在路径.md | 24 ++- 6 files changed, 222 insertions(+), 3 deletions(-) create mode 100644 .obsidian/app.json create mode 100644 .obsidian/appearance.json create mode 100644 .obsidian/core-plugins-migration.json create mode 100644 .obsidian/core-plugins.json create mode 100644 .obsidian/workspace.json diff --git a/.obsidian/app.json b/.obsidian/app.json new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/.obsidian/app.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/.obsidian/appearance.json b/.obsidian/appearance.json new file mode 100644 index 00000000..c8c365d8 --- /dev/null +++ b/.obsidian/appearance.json @@ -0,0 +1,3 @@ +{ + "accentColor": "" +} \ No newline at end of file diff --git a/.obsidian/core-plugins-migration.json b/.obsidian/core-plugins-migration.json new file mode 100644 index 00000000..436f43cf --- /dev/null +++ b/.obsidian/core-plugins-migration.json @@ -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 +} \ No newline at end of file diff --git a/.obsidian/core-plugins.json b/.obsidian/core-plugins.json new file mode 100644 index 00000000..9405bfdc --- /dev/null +++ b/.obsidian/core-plugins.json @@ -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" +] \ No newline at end of file diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json new file mode 100644 index 00000000..144fe03f --- /dev/null +++ b/.obsidian/workspace.json @@ -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" + ] +} \ No newline at end of file diff --git a/problems/1971.寻找图中是否存在路径.md b/problems/1971.寻找图中是否存在路径.md index 89a2a1fe..4123c03e 100644 --- a/problems/1971.寻找图中是否存在路径.md +++ b/problems/1971.寻找图中是否存在路径.md @@ -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是否找到同一个根 From 12fffbeaa86bfc293caf22b4b5fc05d4e929d738 Mon Sep 17 00:00:00 2001 From: wbingb Date: Wed, 15 May 2024 10:39:13 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .obsidian/app.json | 1 - .obsidian/appearance.json | 3 - .obsidian/core-plugins-migration.json | 30 ------ .obsidian/core-plugins.json | 20 ---- .obsidian/workspace.json | 147 -------------------------- 5 files changed, 201 deletions(-) delete mode 100644 .obsidian/app.json delete mode 100644 .obsidian/appearance.json delete mode 100644 .obsidian/core-plugins-migration.json delete mode 100644 .obsidian/core-plugins.json delete mode 100644 .obsidian/workspace.json diff --git a/.obsidian/app.json b/.obsidian/app.json deleted file mode 100644 index 9e26dfee..00000000 --- a/.obsidian/app.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/.obsidian/appearance.json b/.obsidian/appearance.json deleted file mode 100644 index c8c365d8..00000000 --- a/.obsidian/appearance.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "accentColor": "" -} \ No newline at end of file diff --git a/.obsidian/core-plugins-migration.json b/.obsidian/core-plugins-migration.json deleted file mode 100644 index 436f43cf..00000000 --- a/.obsidian/core-plugins-migration.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "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 -} \ No newline at end of file diff --git a/.obsidian/core-plugins.json b/.obsidian/core-plugins.json deleted file mode 100644 index 9405bfdc..00000000 --- a/.obsidian/core-plugins.json +++ /dev/null @@ -1,20 +0,0 @@ -[ - "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" -] \ No newline at end of file diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json deleted file mode 100644 index 144fe03f..00000000 --- a/.obsidian/workspace.json +++ /dev/null @@ -1,147 +0,0 @@ -{ - "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" - ] -} \ No newline at end of file