From 8894e598511d6008253a1636951aadd520150467 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Sat, 30 Aug 2025 13:43:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0P2016.cpp=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E6=9C=80=E5=B0=8F=E9=A1=B6=E7=82=B9=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现了一个动态规划算法来解决树结构的最小顶点覆盖问题。使用深度优先搜索遍历树结构,并通过动态规划计算每个节点的两种状态值,最终输出根节点的最优解。 --- src/8/30/P2016.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/8/30/P2016.cpp diff --git a/src/8/30/P2016.cpp b/src/8/30/P2016.cpp new file mode 100644 index 0000000..2294208 --- /dev/null +++ b/src/8/30/P2016.cpp @@ -0,0 +1,57 @@ +/* + + + +*/ +#include +#include +#include +#include +#include +#include +using ll = int64_t; + +const ll inf = 1e9; +ll n; +std::vector> edg,dp; + +static inline void dfs(ll f,ll u){ + for(ll v:edg[u]){ + if(v==f)continue; + dfs(u,v); + dp[u][1]+=std::min( + dp[v][0], + dp[v][1] + ); + dp[u][0]+=dp[v][1]; + } +} + + +int main(){ + std::iostream::sync_with_stdio(false); + std::cin.tie(nullptr); + + std::cin>>n; + edg.resize(n); + dp.resize(n,std::vector(2,0)); + for(ll i=1;i<=n;++i){ + dp[i-1][1]=1; + ll u,k; + std::cin>>u>>k; + for(ll j=1;j<=k;++j){ + ll v; + std::cin>>v; + edg[u].push_back(v); + edg[v].push_back(u); + } + } + + dfs(0, 0); + // for(ll i=0;i