{"id":525,"date":"2023-04-15T16:09:24","date_gmt":"2023-04-15T08:09:24","guid":{"rendered":"http:\/\/120.55.184.7\/?p=525"},"modified":"2025-04-16T00:44:23","modified_gmt":"2025-04-15T16:44:23","slug":"%e6%a0%91%e5%bd%a2dp","status":"publish","type":"post","link":"https:\/\/beijian99.top\/?p=525","title":{"rendered":"\u6811\u5f62DP"},"content":{"rendered":"\n<p>\u6811\u5f62\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u5728\u6811\u5f62\u6570\u636e\u7ed3\u6784\u4e0a\u5e94\u7528\u52a8\u6001\u89c4\u5212\u601d\u60f3\u7684\u7b97\u6cd5\uff0c\u5b83\u901a\u8fc7\u9012\u5f52\u904d\u5386\u6811\u7ed3\u6784\u5e76\u5229\u7528\u5b50\u95ee\u9898\u7684\u89e3\u6765\u6784\u5efa\u6574\u4f53\u95ee\u9898\u7684\u89e3\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u57fa\u672c\u6982\u5ff5\u4e0e\u7279\u70b9<\/h2>\n\n\n\n<p>\u6811\u5f62DP\u5177\u6709\u4ee5\u4e0b\u7279\u5f81\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5c42\u6b21\u6027<\/strong>\uff1a\u6811\u7ed3\u6784\u5929\u7136\u5177\u6709\u5c42\u6b21\u5173\u7cfb\uff0c\u9002\u5408\u81ea\u5e95\u5411\u4e0a\u6216\u81ea\u9876\u5411\u4e0b\u7684\u9012\u63a8<\/li>\n\n\n\n<li><strong>\u9012\u5f52\u6027<\/strong>\uff1a\u6811\u7684\u5b50\u7ed3\u6784\u6027\u8d28\u4f7f\u5f97\u95ee\u9898\u53ef\u4ee5\u5206\u89e3\u4e3a\u5b50\u95ee\u9898<\/li>\n\n\n\n<li><strong>\u65e0\u540e\u6548\u6027<\/strong>\uff1a\u7236\u8282\u70b9\u7684\u72b6\u6001\u4ec5\u4f9d\u8d56\u4e8e\u5b50\u8282\u70b9\u7684\u72b6\u6001<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u6838\u5fc3\u601d\u60f3\u4e0e\u5b9e\u73b0\u65b9\u5f0f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4e24\u79cd\u904d\u5386\u65b9\u5411<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u53f6\u2192\u6839\uff08\u540e\u5e8f\u904d\u5386\uff09<\/strong>\uff1a\u5148\u5904\u7406\u5b50\u8282\u70b9\u518d\u66f4\u65b0\u7236\u8282\u70b9<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n   void dfs(int u, int parent) {\n       for(int v : tree&#x5B;u]) {\n           if(v != parent) {\n               dfs(v, u);  \/\/ \u5148\u9012\u5f52\u5904\u7406\u5b50\u8282\u70b9\n               \/\/ \u7528\u5b50\u8282\u70b9\u4fe1\u606f\u66f4\u65b0u\u7684\u72b6\u6001\n           }\n       }\n   }\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u6839\u2192\u53f6\uff08\u5148\u5e8f\u904d\u5386\uff09<\/strong>\uff1a\u9884\u5904\u7406\u540e\u518d\u5411\u4e0b\u4f20\u9012\u4fe1\u606f<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n   void dfs(int u, int parent) {\n       \/\/ \u9884\u5904\u7406u\u7684\u72b6\u6001\n       for(int v : tree&#x5B;u]) {\n           if(v != parent) {\n               \/\/ \u4f20\u9012\u4fe1\u606f\u7ed9\u5b50\u8282\u70b9\n               dfs(v, u);\n           }\n       }\n   }\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. \u65f6\u95f4\u590d\u6742\u5ea6<\/h3>\n\n\n\n<p>\u4e00\u822c\u4e3aO(n)\uff0cn\u4e3a\u8282\u70b9\u6570<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u7ecf\u5178\u95ee\u9898\u4e0eC++\u5b9e\u73b0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u6ca1\u6709\u4e0a\u53f8\u7684\u821e\u4f1a\uff08\u6700\u5927\u72ec\u7acb\u96c6\uff09<\/h3>\n\n\n\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u9009\u62e9\u8282\u70b9\u96c6\u5408\uff0c\u4f7f\u5f97\u6ca1\u6709\u4e24\u4e2a\u76f8\u90bb\u8282\u70b9\u88ab\u9009\u4e2d\uff0c\u4e14\u4ef7\u503c\u548c\u6700\u5927<\/p>\n\n\n\n<p><strong>\u72b6\u6001\u5b9a\u4e49<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>dp[u][0]<\/code>\uff1a\u4e0d\u9009u\u65f6\u7684\u6700\u5927\u503c<\/li>\n\n\n\n<li><code>dp[u][1]<\/code>\uff1a\u9009u\u65f6\u7684\u6700\u5927\u503c<\/li>\n<\/ul>\n\n\n\n<p><strong>C++\u5b9e\u73b0<\/strong>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvector&lt;vector&lt;int&gt;&gt; tree;\nvector&lt;int&gt; value;\nvector&lt;vector&lt;int&gt;&gt; dp;\n\nvoid dfs(int u, int parent) {\n    dp&#x5B;u]&#x5B;1] = value&#x5B;u];  \/\/ \u9009\u62e9u\u7684\u521d\u59cb\u503c\n\n    for(int v : tree&#x5B;u]) {\n        if(v == parent) continue;\n        dfs(v, u);\n        dp&#x5B;u]&#x5B;0] += max(dp&#x5B;v]&#x5B;0], dp&#x5B;v]&#x5B;1]);  \/\/ u\u4e0d\u9009\uff0c\u5b50\u8282\u70b9\u53ef\u9009\u53ef\u4e0d\u9009\n        dp&#x5B;u]&#x5B;1] += dp&#x5B;v]&#x5B;0];                 \/\/ u\u9009\u4e86\uff0c\u5b50\u8282\u70b9\u4e0d\u80fd\u9009\n    }\n}\n\nint maxIndependentSet(int root) {\n    dp.assign(n+1, vector&lt;int&gt;(2, 0));\n    dfs(root, -1);\n    return max(dp&#x5B;root]&#x5B;0], dp&#x5B;root]&#x5B;1]);\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. \u6811\u7684\u6700\u957f\u8def\u5f84\uff08\u76f4\u5f84\uff09<\/h3>\n\n\n\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u6c42\u6811\u4e0a\u4efb\u610f\u4e24\u70b9\u95f4\u7684\u6700\u957f\u8def\u5f84<\/p>\n\n\n\n<p><strong>\u89e3\u6cd5<\/strong>\uff1a\u5bf9\u4e8e\u6bcf\u4e2a\u8282\u70b9\uff0c\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>d1[u]<\/code>\uff1au\u5411\u4e0b\u7684\u6700\u957f\u8def\u5f84<\/li>\n\n\n\n<li><code>d2[u]<\/code>\uff1au\u5411\u4e0b\u7684\u6b21\u957f\u8def\u5f84<\/li>\n<\/ul>\n\n\n\n<p><strong>C++\u5b9e\u73b0<\/strong>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvector&lt;vector&lt;pair&lt;int,int&gt;&gt;&gt; tree; \/\/ \u90bb\u63a5\u8868\u5b58\u50a8\u6811\nvector&lt;int&gt; d1, d2; \/\/ \u6700\u957f\u548c\u6b21\u957f\u8def\u5f84\nint diameter = 0;\n\nvoid dfs(int u, int parent) {\n    for(auto &#x5B;v, w] : tree&#x5B;u]) {\n        if(v == parent) continue;\n        dfs(v, u);\n        int len = d1&#x5B;v] + w;\n        if(len &gt; d1&#x5B;u]) {\n            d2&#x5B;u] = d1&#x5B;u];\n            d1&#x5B;u] = len;\n        } else if(len &gt; d2&#x5B;u]) {\n            d2&#x5B;u] = len;\n        }\n    }\n    diameter = max(diameter, d1&#x5B;u] + d2&#x5B;u]);\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. \u6811\u4e0a\u80cc\u5305\uff08\u9009\u8bfe\u95ee\u9898\uff09<\/h3>\n\n\n\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u4f9d\u8d56\u524d\u5bfc\u8bfe\u7a0b\u7684\u9009\u8bfe\u95ee\u9898\uff0c\u6c42\u9009m\u95e8\u8bfe\u7684\u6700\u5927\u4ef7\u503c<\/p>\n\n\n\n<p><strong>C++\u5b9e\u73b0<\/strong>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvector&lt;vector&lt;int&gt;&gt; tree;\nvector&lt;vector&lt;int&gt;&gt; dp; \/\/ dp&#x5B;u]&#x5B;j]: \u4ee5u\u4e3a\u6839\u9009j\u95e8\u8bfe\u7684\u6700\u5927\u503c\nvector&lt;int&gt; credit;\nint m;\n\nvoid dfs(int u) {\n    for(int v : tree&#x5B;u]) {\n        dfs(v);\n        \/\/ \u9006\u5411\u66f4\u65b0\u9632\u6b62\u91cd\u590d\u8ba1\u7b97\n        for(int j = m; j &gt;= 1; j--) {\n            for(int k = 1; k &lt;= j; k++) {\n                dp&#x5B;u]&#x5B;j] = max(dp&#x5B;u]&#x5B;j], dp&#x5B;u]&#x5B;j-k] + dp&#x5B;v]&#x5B;k]);\n            }\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u5e94\u7528\u573a\u666f\u4e0e\u53d8\u79cd<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b9e\u9645\u5e94\u7528<\/strong>\uff1a<br>\u2022 \u7ec4\u7ec7\u7ed3\u6784\u4f18\u5316\uff08\u5982\u5458\u5de5\u6392\u73ed\uff09<br>\u2022 \u7f51\u7edc\u8def\u7531\u4f18\u5316<br>\u2022 \u8d44\u6e90\u5206\u914d\u95ee\u9898<\/li>\n\n\n\n<li><strong>\u5e38\u89c1\u53d8\u79cd\u95ee\u9898<\/strong>\uff1a<br>\u2022 \u6811\u7684\u91cd\u5fc3<br>\u2022 \u6811\u7684\u70b9\u8986\u76d6<br>\u2022 \u6811\u7684\u8fb9\u8986\u76d6<br>\u2022 \u6811\u7684\u5339\u914d\u95ee\u9898<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u89e3\u9898\u6280\u5de7<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u72b6\u6001\u8bbe\u8ba1\u8981\u70b9<\/strong>\uff1a<br>\u2022 \u901a\u5e38\u4ee5<code>dp[u][k]<\/code>\u8868\u793a\u4ee5u\u4e3a\u6839\u7684\u5b50\u6811\u5728\u72b6\u6001k\u4e0b\u7684\u6700\u4f18\u89e3<br>\u2022 \u8003\u8651\u7236\u5b50\u8282\u70b9\u95f4\u7684\u7ea6\u675f\u5173\u7cfb<\/li>\n\n\n\n<li><strong>\u5b9e\u73b0\u6280\u5de7<\/strong>\uff1a<br>\u2022 \u4f7f\u7528\u90bb\u63a5\u8868\u6216\u94fe\u5f0f\u524d\u5411\u661f\u5b58\u50a8\u6811<br>\u2022 \u6ce8\u610f\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff08\u8bb0\u5fc6\u5316\uff09<br>\u2022 \u5904\u7406\u68ee\u6797\u65f6\u6dfb\u52a0\u865a\u62df\u6839\u8282\u70b9<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6811\u5f62\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u5728\u6811\u5f62\u6570\u636e\u7ed3\u6784\u4e0a\u5e94\u7528\u52a8\u6001\u89c4\u5212\u601d\u60f3\u7684\u7b97\u6cd5\uff0c\u5b83\u901a\u8fc7\u9012\u5f52\u904d\u5386\u6811\u7ed3\u6784\u5e76\u5229\u7528\u5b50\u95ee\u9898\u7684\u89e3\u6765\u6784\u5efa\u6574\u4f53\u95ee\u9898\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[4],"tags":[120],"class_list":["post-525","post","type-post","status-publish","format-standard","hentry","category-algorithm","tag-dp"],"_links":{"self":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=525"}],"version-history":[{"count":1,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/525\/revisions"}],"predecessor-version":[{"id":526,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/525\/revisions\/526"}],"wp:attachment":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}