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是否找到同一个根