HDU 1054 Strategic Game (树形DP)
来源:互联网 发布:cdn加速 知乎 编辑:程序博客网 时间:2024/04/30 12:39
dp[0][i]表示在i点上不放置一个士兵时的最小值,dp[1][i]表示在i点上放置一个士兵时的最小值。那么状态转移方程:
dp[0][u]=sum(dp[1][v]),dp[1][v]=1+sum(min(dp[1][v],dp[0][v]);
代码如下:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)const int mod=100000000;const int INF=0x3f3f3f3f;const double eqs=1e-8;int dp[3][3000], head[3000], cnt;struct node{ int u, v, next;}edge[100000];void add(int u, int v){ edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++;}void dfs(int u, int fa){ dp[1][u]=1; dp[0][u]=0; for(int i=head[u];i+1;i=edge[i].next){ int v=edge[i].v; if(v==fa) continue ; dfs(v,u); dp[0][u]+=dp[1][v]; dp[1][u]+=min(dp[1][v],dp[0][v]); }}void init(){ memset(head,-1,sizeof(head)); cnt=0;}int main(){ int n, i, j, m, u, v; while(scanf("%d",&n)!=EOF){ init(); for(i=0;i<n;i++){ scanf("%d:(%d)",&u,&m); while(m--){ scanf("%d",&v); add(u,v); add(v,u); } } dfs(0,-1); printf("%d\n",min(dp[0][0],dp[1][0])); } return 0;}
1 0
- hdu 1054 Strategic Game 树形dp
- Strategic Game - HDU 1054 树形dp
- hdu 1054 Strategic Game 树形dp
- hdu 1054 Strategic Game(树形DP)
- HDU 1054 Strategic Game(树形DP)
- hdu - 1054 - Strategic Game(树形dp)
- HDU 1054 Strategic Game (树形DP)
- HDU 1054 Strategic Game (树形DP)
- HDU-1054 Strategic Game(树形DP)
- hdu 1054 Strategic Game 树形DP
- HDU 1054 Strategic Game (树形DP)
- HDU 1054 Strategic Game (树形DP)
- HDU 1054Strategic Game 树形DP
- HDU 1054 Strategic Game (树形DP)
- Strategic Game+hdu+树形dp
- 1054 Strategic Game 树形DP
- HDU 1054 Strategic Game 树形DP/二分图匹配
- hdu 1054 Strategic Game 树形dp基础&&模板
- 微软3个月没能修复零日漏洞被谷歌公布给黑客,发布攻击代码教人们怎么攻击
- 动态规划算法思想
- 程序猿常用目录整理方案
- 精通CSS+DIV网页样式与布局--滤镜的使用
- POJ 2159 Ancient Cipher
- HDU 1054 Strategic Game (树形DP)
- 响声电话吸费代理 回报增值电话 菲亚科技
- new day
- windows SEH机制注释(1) 基于ReactOS [第二次修订]
- Cut the tree _ HackerRank
- I'm Back!
- MySQL安装时apply security settings错误
- 使用Sqoop1.4.4将MySQL数据库表中数据导入到HDFS中
- 手把手教你把Vim改装成一个IDE编程环境(图文)