{"id":62,"date":"2026-03-27T10:00:11","date_gmt":"2026-03-27T02:00:11","guid":{"rendered":"https:\/\/geocommunity.vip\/?p=62"},"modified":"2026-04-10T12:30:17","modified_gmt":"2026-04-10T04:30:17","slug":"%e5%9b%be%e8%ae%ba%ef%bc%9a%e9%82%bb%e6%8e%a5%e8%a1%a8%e5%ad%98%e5%82%a8%e7%9a%84%e4%ba%8c%e5%8f%89%e6%a0%91","status":"publish","type":"post","link":"https:\/\/geocommunity.vip\/index.php\/2026\/03\/27\/%e5%9b%be%e8%ae%ba%ef%bc%9a%e9%82%bb%e6%8e%a5%e8%a1%a8%e5%ad%98%e5%82%a8%e7%9a%84%e4%ba%8c%e5%8f%89%e6%a0%91\/","title":{"rendered":"\u56fe\u8bba\uff1a\u90bb\u63a5\u8868\u5b58\u50a8\u7684\u4e8c\u53c9\u6811\uff08C\uff09"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u4e00\u3001\u5197\u4f59\u8fde\u63a5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u9898\u76ee\u63cf\u8ff0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6811\u53ef\u4ee5\u770b\u6210\u662f\u4e00\u4e2a\u8fde\u901a\u4e14<strong>\u65e0\u73af<\/strong>\u7684<strong>\u65e0\u5411<\/strong>\u56fe\u3002\u7ed9\u5b9a\u4e00\u4e2a\u56fe\uff0c\u8be5\u56fe\u4ece\u4e00\u68f5 n \u4e2a\u8282\u70b9\uff08\u8282\u70b9\u503c 1\uff5en\uff09\u7684\u6811\u4e2d\u6dfb\u52a0\u4e00\u6761\u8fb9\u540e\u83b7\u5f97\u3002\u6dfb\u52a0\u7684\u8fb9\u7684\u4e24\u4e2a\u4e0d\u540c\u9876\u70b9\u7f16\u53f7\u5728 1 \u5230 n \u4e2d\u95f4\uff0c\u4e14\u8fd9\u6761\u9644\u52a0\u7684\u8fb9\u4e0d\u5c5e\u4e8e\u6811\u4e2d\u5df2\u5b58\u5728\u7684\u8fb9\u3002\u56fe\u7684\u4fe1\u606f\u8bb0\u5f55\u4e8e\u957f\u5ea6\u4e3a n \u7684\u4e8c\u7ef4\u6570\u7ec4 <code>edges<\/code>\uff0c<code>edges[i] = [ai, bi]<\/code> \u8868\u793a\u56fe\u4e2d\u5728 ai \u548c bi \u4e4b\u95f4\u5b58\u5728\u4e00\u6761\u8fb9\u3002\u8bf7\u627e\u51fa\u4e00\u6761\u53ef\u4ee5\u5220\u53bb\u7684\u8fb9\uff0c\u5220\u9664\u540e\u53ef\u4f7f\u5f97\u5269\u4f59\u90e8\u5206\u662f\u4e00\u4e2a\u6709\u7740 n \u4e2a\u8282\u70b9\u7684\u6811\u3002\u5982\u679c\u6709\u591a\u4e2a\u7b54\u6848\uff0c\u5219\u8fd4\u56de\u6570\u7ec4 <code>edges<\/code> \u4e2d<strong>\u6700\u540e\u51fa\u73b0\u7684<\/strong>\u90a3\u4e2a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u601d\u8def<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u904d\u5386\u6bcf\u4e00\u6761\u8fb9\uff0c\u5728\u52a0\u5165\u8fd9\u6761\u8fb9\u4e4b\u524d\uff0c\u5148\u7528 DFS\/BFS \u68c0\u6d4b\u4e24\u4e2a\u8282\u70b9\u662f\u5426\u5df2\u7ecf\u8fde\u901a\u3002\u5982\u679c\u5df2\u7ecf\u8fde\u901a\uff0c\u8bf4\u660e\u8fd9\u6761\u8fb9\u5c31\u662f\u591a\u4f59\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u7b97\u6cd5\u6c42\u89e3<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff081\uff09DFS\u505a\u6cd5<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"971\" height=\"866\" src=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-19.png\" alt=\"\" class=\"wp-image-63\" style=\"aspect-ratio:1.1212614190964836;width:762px;height:auto\" srcset=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-19.png 971w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-19-300x268.png 300w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-19-768x685.png 768w\" sizes=\"auto, (max-width: 971px) 100vw, 971px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">                                                                                \uff08\u4e3b\u51fd\u6570\uff09<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"822\" height=\"542\" src=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-20.png\" alt=\"\" class=\"wp-image-64\" style=\"aspect-ratio:1.5166518556133892;width:593px;height:auto\" srcset=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-20.png 822w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-20-300x198.png 300w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-20-768x506.png 768w\" sizes=\"auto, (max-width: 822px) 100vw, 822px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">                                                               \uff08DFS\u51fd\u6570\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u84dd\u6865\u676f\u5fc5\u4f1a\u5199\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdlib.h>\n#include &lt;stdbool.h>\n#include &lt;string.h>\n#include &lt;stdlib.h>\n\n#define MAX_NODES 1005\n\n<strong>\/\/ \u521d\u59cb\u5316\u90bb\u63a5\u77e9\u9635\uff08\u76f4\u63a5\u5168\u5c40\u5b9a\u4e49\uff09\nint adj&#91;1001]&#91;1001];<\/strong>\n\nbool canReachDFS(int curr, int target, bool* visited) {\n    \/\/ \u627e\u5230\u4e86\u76ee\u6807\uff0c\u8fd4\u56de true\n    if (curr == target) return true;\n\n    \/\/ \u6807\u8bb0\u5f53\u524d\u8282\u70b9\u5df2\u8bbf\u95ee\n    visited&#91;curr] = true;\n\n    <strong>\/\/ \u904d\u5386\u5f53\u524d\u8282\u70b9\u7684\u6240\u6709\u90bb\u5c45<\/strong>\n    for (int i = 1; i &lt; 1001; i++) {\n\n        if (adj&#91;curr]&#91;i] == 1) {\n            int neighbor = i;\n            \/\/ \u5982\u679c\u90bb\u5c45\u8fd8\u6ca1\u8bbf\u95ee\u8fc7\uff0c\u9012\u5f52\u53bb\u67e5\n            if (!visited&#91;neighbor]) {\n                if (canReachDFS(neighbor, target, visited)) {\n                    return true;\n                }\n            }\n        }\n    }\n\n    return false;\n}\n\n\/**\n * Note: The returned array must be malloced, assume caller calls free().\n *\/\nint* findRedundantConnection(int** edges, int edgesSize, int* edgesColSize, int* returnSize) {\n    int* result = (int*)malloc(2 * sizeof(int));\n    *returnSize = 2;\n\n    memset(adj, 0, sizeof(adj));\n    \n\n    for (int i = 0; i &lt; edgesSize; i++) {\n        int u = edges&#91;i]&#91;0];\n        int v = edges&#91;i]&#91;1];\n\n        \/\/ \u6bcf\u6b21 DFS \u524d\u91cd\u7f6e visited \u6570\u7ec4\n        bool visited&#91;MAX_NODES] = {false};\n\n        \/\/ \u6838\u5fc3\uff1a\u7528 DFS \u68c0\u6d4b u \u5230 v \u662f\u5426\u5b58\u5728\u8def\u5f84\n        if (canReachDFS(u, v, visited)) {\n            result&#91;0] = u;\n            result&#91;1] = v;\n            return result;\n        }\n\n<strong>        \/\/ \u6ca1\u8fde\u901a\uff0c\u5219\u52a0\u8fb9\n        adj&#91;u]&#91;v] = 1;\n        adj&#91;v]&#91;u] = 1;<\/strong>\n    }\n\n    return result;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\uff082\uff09BFS\u505a\u6cd5<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"924\" src=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-21-1024x924.png\" alt=\"\" class=\"wp-image-65\" style=\"aspect-ratio:1.1085907534952777;width:784px;height:auto\" srcset=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-21-1024x924.png 1024w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-21-300x271.png 300w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-21-768x693.png 768w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-21-1536x1386.png 1536w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-21.png 1756w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">                                                                                     \uff08\u4e3b\u51fd\u6570\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u89c1\uff0cBFS\u7684\u903b\u8f91\u548cDFS\u7684\u903b\u8f91\u76f8\u540c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"817\" src=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-22.png\" alt=\"\" class=\"wp-image-66\" style=\"aspect-ratio:0.9180039569136074;width:587px;height:auto\" srcset=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-22.png 750w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-22-275x300.png 275w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">                                                            \uff08BFS\u51fd\u6570\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff1a\u8981\u638c\u63e1BFS\u4e2d\u961f\u5217\u7684\u5b58\u50a8\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001\u90bb\u63a5\u8868\u5b58\u50a8\u4e8c\u53c9\u6811\u5b8c\u6574\u505a\u6cd5<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"561\" src=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-23-1024x561.png\" alt=\"\" class=\"wp-image-67\" srcset=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-23-1024x561.png 1024w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-23-300x164.png 300w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-23-768x421.png 768w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-23-1536x842.png 1536w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-23.png 1712w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5197\u4f59\u8fde\u63a5 1\u3001\u9898\u76ee\u63cf\u8ff0 \u6811\u53ef\u4ee5\u770b\u6210\u662f\u4e00\u4e2a\u8fde\u901a\u4e14 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":4,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":151,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/62\/revisions\/151"}],"wp:attachment":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}