Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1971.寻找图中是否存在路径:解决栈溢出问题 #2525

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .obsidian/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions .obsidian/appearance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"accentColor": ""
}
30 changes: 30 additions & 0 deletions .obsidian/core-plugins-migration.json
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions .obsidian/core-plugins.json
Original file line number Diff line number Diff line change
@@ -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 changes: 147 additions & 0 deletions .obsidian/workspace.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
24 changes: 21 additions & 3 deletions problems/1971.寻找图中是否存在路径.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -84,6 +95,8 @@ void join(int u, int v) {

此时我们就可以直接套用并查集模板。

本题在join函数调用find函数时如果是递归调用会发生栈溢出提示,建议使用迭代方法

使用 join(int u, int v)将每条边加入到并查集。

最后 isSame(int u, int v) 判断是否是同一个根 就可以了。
Expand All @@ -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是否找到同一个根
Expand Down