{"id":7519,"date":"2023-07-17T20:14:13","date_gmt":"2023-07-17T12:14:13","guid":{"rendered":"https:\/\/www.5x44.cn\/?p=7519"},"modified":"2023-07-17T20:14:13","modified_gmt":"2023-07-17T12:14:13","slug":"%e6%a0%91%e4%b8%ad%e6%af%8f%e4%b8%aa%e8%8a%82%e7%82%b9%e5%88%b0%e6%a0%b9%e7%9a%84%e8%b7%9d%e7%a6%bb%e4%b9%8b%e5%92%8c","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=7519","title":{"rendered":"\u6811\u4e2d\u6bcf\u4e2a\u8282\u70b9\u5230\u6839\u7684\u8ddd\u79bb\u4e4b\u548c"},"content":{"rendered":"\n<p>\u95ee\u9898\uff1a<\/p>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5411\u3001\u8fde\u901a\u7684\u6811\u3002\u6811\u4e2d\u6709 n \u4e2a\u6807\u8bb0\u4e3a 0\u2026n-1 \u7684\u8282\u70b9\u4ee5\u53ca n-1&nbsp;\u6761\u8fb9&nbsp;\u3002<\/p>\n\n\n\n<p>\u7ed9\u5b9a\u6574\u6570 n \u548c\u6570\u7ec4\u00a0edges\u00a0\uff0c\u00a0edges[i] = [a<sub>i<\/sub>, b<sub>i<\/sub>]\u8868\u793a\u6811\u4e2d\u7684\u8282\u70b9\u00a0a<sub>i<\/sub>\u00a0\u548c\u00a0b<sub>i<\/sub>\u00a0\u4e4b\u95f4\u6709\u4e00\u6761\u8fb9\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4&nbsp;answer&nbsp;\uff0c\u5176\u4e2d&nbsp;answer[i]&nbsp;\u662f\u6811\u4e2d\u7b2c i \u4e2a\u8282\u70b9\u4e0e\u6240\u6709\u5176\u4ed6\u8282\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u4e4b\u548c\u3002<\/p>\n\n\n\n<p>\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09<br>\u94fe\u63a5\uff1ahttps:\/\/leetcode.cn\/problems\/sum-of-distances-in-tree<br>\u8457\u4f5c\u6743\u5f52\u9886\u6263\u7f51\u7edc\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u5b98\u65b9\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"304\" height=\"224\" src=\"https:\/\/www.5x44.cn\/wp-content\/uploads\/2023\/07\/lc-sumdist1.jpg\" alt=\"\" class=\"wp-image-7520\" srcset=\"https:\/\/www.5x44.cn\/wp-content\/uploads\/2023\/07\/lc-sumdist1.jpg 304w, https:\/\/www.5x44.cn\/wp-content\/uploads\/2023\/07\/lc-sumdist1-300x221.jpg 300w\" sizes=\"auto, (max-width: 304px) 100vw, 304px\" \/><\/figure>\n\n\n\n<p>\u8f93\u5165: n = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]]<br>\u8f93\u51fa: [8,12,6,10,10,10]<br>\u89e3\u91ca: \u6811\u5982\u56fe\u6240\u793a\u3002<br>\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97\u51fa dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5)<br>\u4e5f\u5c31\u662f 1 + 1 + 2 + 2 + 2 = 8\u3002 \u56e0\u6b64\uff0canswer[0] = 8\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    def sumOfDistancesInTree(self, n: int, edges: list&#91;list&#91;int]]) -> list&#91;int]:\n        relationTree=&#91;&#91;] for _ in range(n)]\n        sonNode=&#91;0]*n\n        dp=&#91;0]*n\n        result=&#91;0]*n\n\n#\u6211\u8ba4\u4e3a\u8fd9\u4e2a\u7b97\u6cd5\u7684\u7b2c\u4e00\u4e2a\u96be\u70b9\u662f\u91cd\u5efa\u5173\u7cfb\uff0c\u56e0\u4e3a\u662f\u4e2a\u65e0\u5411\u6811\u578b\u56fe\uff0c\u6240\u4ee5&#91;0,1]\u4e2d0\u548c1\u90fd\u53ef\u4ee5\u770b\u505a\u662f\u53e6\u4e00\u65b9\u7684\u6839\u3002\u4e0b\u9762\u8fd9\u4e2arelationTree\u5c31\u662f\u628a\u8fd9\u4e9b\u5173\u7cfb\u91cd\u5efa\u3002<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u7d22\u5f15\u4e3a\u6839\u8282\u70b9\uff0c\u503c\u4e3a\u5b50\u6811<\/mark><\/strong>\u3002\n        for f,s in edges:\n            relationTree&#91;f].append(s)\n            relationTree&#91;s].append(f)\n\n        def dfs0(root:int, father:int):\n            #sonNode&#91;i]\u4fdd\u5b58\u7740i\u8282\u70b9\u6709\u591a\u5c11\u4e2a\u5b50\u8282\u70b9\n            sonNode&#91;root]=1\n            #dp&#91;i]\u4fdd\u5b58\u7740i\u8282\u70b9\u7684\u5b50\u8282\u70b9\u5230i\u7684\u4e3e\u4f8b\u548c\u3002\u8ba1\u7b97\u8fc7\u7a0b\u5fc5\u987b\u7528\u6df1\u5ea6\u4f18\u5148\u7684\u65b9\u5f0f\u4ece\u6700\u4e0b\u5c42\u7684\u5b50\u6811\u5f00\u59cb\u8ba1\u7b97\u3002\n            dp&#91;root]=0\n            for item in relationTree&#91;root]:\n                if item==father:\n                    continue\n                dfs0(item,root)\n                #dfs0\u51fd\u6570\u8fd4\u56de\u4ee3\u8868\u5f53\u8def\u5f84\u5df2\u7ecf\u5230\u4e86\u53f6\u5b50\u8282\u70b9\uff0citem\u5c31\u662f\u53f6\u5b50\u8282\u70b9\uff0croot\u662f\u53f6\u5b50\u8282\u70b9\u7684\u7236\u8282\u70b9\u3002\n                dp&#91;root]+=dp&#91;item]+sonNode&#91;item]\n                sonNode&#91;root]+=sonNode&#91;item]\n        dfs0(0,-1)\n        #dfs1\u7684\u4f5c\u7528\u662f\u6362\u6839\uff0c\u5c06\u4e0e\u539f\u6765\u7684\u6839\u76f8\u8fde\u63a5\u7684\u8282\u70b9\u53d8\u6210\u65b0\u7684\u6839\uff0c\u5177\u4f53\u7684\u4ea4\u6362\u8fc7\u7a0b\u53ef\u4ee5\u770b\u529b\u6263834\u53f7\u9898\u76ee\u7684\u89e3\u9898\u90e8\u5206\u7684\u52a8\u753b\u3002\n        def dfs1(root:int, father:int):\n            #\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u662f\u4f7f\u7528\u6df1\u5ea6\u4f18\u5148\u7b97\u6cd5\uff0c\u56e0\u4e3a\u4e0d\u60f3\u5bf9\u6bcf\u4e2a\u8282\u70b9\u91cd\u65b0\u8ba1\u7b97\u4e00\u6b21\uff0c\u672c\u51fd\u6570\u662f\u901a\u8fc7\u4f7f\u7528dfs0\u7684\u7ed3\u679c\"sonNode\"\u548c\"dp\"\u6765\u751f\u6210\u4ee5\u5176\u5b83\u8282\u70b9\u4e3a\u6839\u65f6\u7684\u7ed3\u679c\u3002\u56e0\u4e3a\u4e4b\u524d\u5df2\u7ecf\u628a0\u8282\u70b9\u4e3a\u6839\u65f6\u7684\u7ed3\u679c\u8ba1\u7b97\u51fa\u6765\u4e86\uff0c\u6240\u4ee5\u4fdd\u5b58\u8be5\u503c\u5230result&#91;root]\u4e2d\n            result&#91;root]=dp&#91;root]\n            for item in relationTree&#91;root]:\n                if item==father:\n                    continue\n               #\u4fdd\u5b58\u5f53\u524d\u7684\u503c\uff0c\u56e0\u4e3a\u4fee\u6539\u8fc7\u540e\u8fd8\u8981\u6062\u590d\uff0c\u56e0\u4e3a\u8981\u4fdd\u63010\u4e3a\u6839\u65f6\u7684\u6240\u6709\u6570\u636e\uff0c\u5176\u5b83\u7684\u503c\u90fd\u662f\u6839\u636e\u5b83\u751f\u6210\u7684\u3002\n                pd,pn = dp&#91;root],sonNode&#91;root]\n                sd,sn = dp&#91;item],sonNode&#91;item]\n                dp&#91;root] -= (dp&#91;item]+sonNode&#91;item])\n                sonNode&#91;root]-=sonNode&#91;item]\n                dp&#91;item]+=dp&#91;root]+sonNode&#91;root]\n                sonNode&#91;item]=n\n                dfs1(item,root)\n                dp&#91;root],sonNode&#91;root] = pd,pn\n                dp&#91;item],sonNode&#91;item] = sd,sn\n\n        dfs1(0,-1)\n\n        return result<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\uff1a \u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5411\u3001\u8fde\u901a\u7684\u6811\u3002\u6811\u4e2d\u6709 n \u4e2a\u6807\u8bb0\u4e3a 0\u2026n-1 \u7684\u8282\u70b9\u4ee5\u53ca n-1&nbsp;\u6761\u8fb9&nbsp;\u3002 \u7ed9\u5b9a\u6574\u6570 n \u548c\u6570\u7ec4\u00a0edges\u00a0\uff0c\u00a0edg&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=7519\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-7519","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7519"}],"version-history":[{"count":1,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7519\/revisions"}],"predecessor-version":[{"id":7521,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7519\/revisions\/7521"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}