{"id":2972,"date":"2022-03-18T11:13:18","date_gmt":"2022-03-18T03:13:18","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2972"},"modified":"2022-03-24T16:32:18","modified_gmt":"2022-03-24T08:32:18","slug":"hierholzer","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/03\/18\/hierholzer\/","title":{"rendered":"Hierholzer \u7b97\u6cd5 &#8212;\u6709\u5411\u56fe\u6c42\u89e3\u6b27\u62c9\u8def\u5f84\uff08\u73af\u8def\uff09"},"content":{"rendered":"\n<ul><li><strong>\u6b27\u62c9\u8def\u5f84<\/strong>\uff1a<br>\u5982\u679c\u5728\u4e00\u5f20\u56fe\u4e2d\uff0c\u53ef\u4ee5\u4ece\u4e00\u70b9\u51fa\u53d1\u904d\u5386\u6240\u6709\u7684\u8fb9\uff0c\u90a3\u4e48\u904d\u5386\u8fc7\u7a0b\u4e2d\u7684\u8fd9\u6761\u8def\u5f84\u5c31\u53eb\u505a\u6b27\u62c9\u8def\u5f84\u3002\u5982\u679c\u8fd9\u6761\u8def\u5f84\u662f\u95ed\u5408\u7684\uff0c\u90a3\u5c31\u79f0\u4e3a<strong>\u6b27\u62c9\u56de\u8def<\/strong>\u3002<br>\u7b80\u5355\u5730\u8bf4\uff0c\u5982\u679c\u4e00\u5f20\u56fe\u53ef\u4ee5\u88ab\u201c\u4e00\u7b14\u753b\u201d\uff0c\u90a3\u4e48\u201c\u4e00\u7b14\u753b\u201d\u7684\u90a3\u4e2a\u8f68\u8ff9\u5c31\u53eb\u505a\u6b27\u62c9\u8def\u5f84\u3002<\/li><li><strong>\u6b27\u62c9\u56fe\u5224\u5b9a\u5b9a\u7406<\/strong>\uff1a<br>\u542b\u6709\u6b27\u62c9\u56de\u8def\u7684\u56fe\u79f0\u4e3a\u6b27\u62c9\u56fe\uff0c\u542b\u6709\u6b27\u62c9\u8def\u5f84\u7684\u56fe\u79f0\u4e3a\u534a\u6b27\u62c9\u56fe\u3002<br>\u65e0\u5411\u56fe\u4e2d\uff0c\u5982\u679c\u6240\u6709\u9876\u70b9\u7684\u5ea6\u6570\u90fd\u4e3a\u5076\u6570\uff0c\u5219\u4e3a\u6b27\u62c9\u56fe\uff1b\u5982\u679c\u6709\u4e24\u4e2a\u9876\u70b9\u7684\u5ea6\u6570\u4e3a\u5947\u6570\uff0c\u5176\u4ed6\u7684\u4e3a\u5076\u6570\uff0c\u5219\u4e3a\u534a\u6b27\u62c9\u56fe\u3002<br>\u6709\u5411\u56fe\u4e2d\uff0c\u5982\u679c\u6240\u6709\u9876\u70b9\u7684\u5165\u5ea6\u7b49\u4e8e\u51fa\u5ea6\uff0c\u90a3\u4e48\u5c31\u662f\u6b27\u62c9\u56fe\u3002\u5224\u5b9a\u534a\u6b27\u62c9\u56fe\u7684\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\uff0c\u5927\u5bb6\u53ef\u4ee5\u81ea\u884c\u63a8\u7406\u3002<\/li><\/ul>\n\n\n\n<h2>Hierholzer \u7b97\u6cd5<\/h2>\n\n\n\n<p><strong>\u95ee\u9898\u7b80\u8ff0<\/strong>\uff1a\u73b0\u7ed9\u51fa\u4e00\u4e2a\u6709\u5411\u56fe\uff0c\u4e14\u4e3a\u6b27\u62c9\u56fe\u3002\u6c42\u6b27\u62c9\u56de\u8def\u3002<\/p>\n\n\n\n<p><strong>Hierholzer \u7b97\u6cd5\u8fc7\u7a0b<\/strong>\uff1a<\/p>\n\n\n\n<ol><li>\u9009\u62e9\u4efb\u4e00\u9876\u70b9\u4e3a\u8d77\u70b9\uff0c\u904d\u5386\u6240\u6709\u76f8\u90bb\u8fb9\u3002<\/li><li>\u6df1\u5ea6\u641c\u7d22\uff0c\u8bbf\u95ee\u76f8\u90bb\u9876\u70b9\u3002\u5c06\u7ecf\u8fc7\u7684\u8fb9\u90fd\u5220\u9664\u3002<\/li><li>\u5982\u679c\u5f53\u524d\u9876\u70b9\u6ca1\u6709\u76f8\u90bb\u8fb9\uff0c\u5219\u5c06\u9876\u70b9\u5165\u6808\u3002<\/li><li>\u6808\u4e2d\u7684\u9876\u70b9\u5012\u5e8f\u8f93\u51fa\uff0c\u5c31\u662f\u4ece\u8d77\u70b9\u51fa\u53d1\u7684\u6b27\u62c9\u56de\u8def\u3002<\/li><\/ol>\n\n\n\n<p>\u4ece\u4e00\u4e2a\u53ef\u80fd\u7684\u8d77\u70b9\u51fa\u53d1\uff0c\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u4f46\u662f\u6bcf\u6b21\u6cbf\u7740\u8f85\u52a9\u8fb9\u4ece\u67d0\u4e2a\u9876\u70b9\u79fb\u52a8\u5230\u53e6\u5916\u4e00\u4e2a\u9876\u70b9\u7684\u65f6\u5019\uff0c\u90fd\u9700\u8981\u5220\u9664\u8fd9\u4e2a\u8f85\u52a9\u8fb9\u3002\u5982\u679c\u6ca1\u6709\u53ef\u79fb\u52a8\u7684\u8def\u5f84\uff0c\u5219\u5c06\u6240\u5728\u7ed3\u70b9\u52a0\u5165\u5230\u6808\u4e2d\uff0c\u5e76\u8fd4\u56de\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dfs(node, trace){\n\twhile(!node.adj.isEmpty()){\n\t\tNode next = node.adj.removeLast();\n\t\tdfs(next, trace);\n\t}\n\ttrace.addLast(node);\n}\n<\/code><\/pre>\n\n\n\n<p>\u6700\u540e\u5f97\u5230\u7684\u6808\u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6574\u4e2a\u6b27\u62c9\u95ed\u8ff9\u4e2d\u7684\u9876\u70b9\u3002\uff08\u8981\u6062\u590d\u6211\u4eec\u9700\u8981\u4e0d\u65ad\u51fa\u6808\uff0c\u56e0\u6b64\u5982\u679c\u4f60\u7528\u5217\u8868\u6765\u5b58\u6b27\u5783\u8ff9\u7684\u8bdd\u9700\u8981\u53cd\u8f6c\u4e00\u6b21\uff09\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/reconstruct-itinerary\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u91cd\u65b0\u5b89\u6392\u884c\u7a0b<\/a>\uff1a\u7ed9\u4f60\u4e00\u4efd\u822a\u7ebf\u5217\u8868 tickets \uff0c\u5176\u4e2d tickets[i] = [fromi, toi] \u8868\u793a\u98de\u673a\u51fa\u53d1\u548c\u964d\u843d\u7684\u673a\u573a\u5730\u70b9\u3002\u8bf7\u4f60\u5bf9\u8be5\u884c\u7a0b\u8fdb\u884c\u91cd\u65b0\u89c4\u5212\u6392\u5e8f\u3002\u6240\u6709\u8fd9\u4e9b\u673a\u7968\u90fd\u5c5e\u4e8e\u4e00\u4e2a\u4ece JFK\uff08\u80af\u5c3c\u8fea\u56fd\u9645\u673a\u573a\uff09\u51fa\u53d1\u7684\u5148\u751f\uff0c\u6240\u4ee5\u8be5\u884c\u7a0b\u5fc5\u987b\u4ece JFK \u5f00\u59cb\u3002\u5982\u679c\u5b58\u5728\u591a\u79cd\u6709\u6548\u7684\u884c\u7a0b\uff0c\u8bf7\u4f60\u6309\u5b57\u5178\u6392\u5e8f\u8fd4\u56de\u6700\u5c0f\u7684\u884c\u7a0b\u7ec4\u5408\u3002\u4f8b\u5982\uff0c\u884c\u7a0b [\u201cJFK\u201d, \u201cLGA\u201d] \u4e0e [\u201cJFK\u201d, \u201cLGB\u201d] \u76f8\u6bd4\u5c31\u66f4\u5c0f\uff0c\u6392\u5e8f\u66f4\u9760\u524d\u3002<br>\u5047\u5b9a\u6240\u6709\u673a\u7968\u81f3\u5c11\u5b58\u5728\u4e00\u79cd\u5408\u7406\u7684\u884c\u7a0b\u3002\u4e14\u6240\u6709\u7684\u673a\u7968 \u5fc5\u987b\u90fd\u7528\u4e00\u6b21\u4e14\u53ea\u80fd\u7528\u4e00\u6b21\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/img-blog.csdnimg.cn\/bb1027c94c684bcc9bbf7d106ab0bf44.png?x-oss-process=image\/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAam9obmppbTA4MTY=,size_20,color_FFFFFF,t_70,g_se,x_16\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    ''' Hierholzer \u7b97\u6cd5\n    '''\n    def findItinerary(self, tickets):\n        def dfs(cur, graph, res):\n            while graph&#91;cur]: # \u904d\u5386\u6240\u6709\u8fb9\n                dfs(graph&#91;cur].pop(), graph, res) # \u8bbf\u95ee\u5e76\u5220\u9664\n            if not graph&#91;cur]: # \u5c06\u9876\u70b9\u5165\u6808\n                res.append(cur)\n        import collections\n        # \u5efa\u56fe\n        graph = collections.defaultdict(list)\n        for start, end in sorted(tickets)&#91;::-1]:\n            graph&#91;start].append(end)\n        res = &#91;]\n        dfs(\"JFK\", graph, res)\n        return res&#91;::-1] # \u5012\u5e8f\u8f93\u51fa<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6b27\u62c9\u8def\u5f84\uff1a\u5982\u679c\u5728\u4e00\u5f20\u56fe\u4e2d\uff0c\u53ef\u4ee5\u4ece\u4e00\u70b9\u51fa\u53d1\u904d\u5386\u6240\u6709\u7684\u8fb9\uff0c\u90a3\u4e48\u904d\u5386\u8fc7\u7a0b\u4e2d\u7684\u8fd9\u6761\u8def\u5f84\u5c31\u53eb\u505a\u6b27\u62c9\u8def\u5f84\u3002\u5982\u679c\u8fd9\u6761\u8def\u5f84\u662f\u95ed &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/03\/18\/hierholzer\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Hierholzer \u7b97\u6cd5 &#8212;\u6709\u5411\u56fe\u6c42\u89e3\u6b27\u62c9\u8def\u5f84\uff08\u73af\u8def\uff09<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,8],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2972"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2972"}],"version-history":[{"count":6,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2972\/revisions"}],"predecessor-version":[{"id":2978,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2972\/revisions\/2978"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2972"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}