UVALive2038(树形dp)
2016-08-12 20:09
459 查看
链接:点击打开链接
题意:给出一棵树,求在节点上最少放几个灯,使得所有边都被覆盖
代码:
题意:给出一棵树,求在节点上最少放几个灯,使得所有边都被覆盖
代码:
#include <vector> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; int dp[2005][2],vis[2005]; vector<int> G[2005]; void dfs(int s){ //dp[i][j]表示i点事否放灯 int i,tmp; vis[s]=1; dp[s][0]=0; dp[s][1]=1; for(i=0;i<G[s].size();i++){ tmp=G[s][i]; if(vis[tmp]) continue; dfs(tmp); dp[s][0]+=dp[tmp][1]; //父节点不放子节点必须放 dp[s][1]+=min(dp[tmp][1],dp[tmp][0]); //父节点放子节点可以不放 } } int main(){ int n,i,j,x,y,num; while(scanf("%d",&n)!=EOF){ for(i=0;i<=n;i++) G[i].clear(); for(i=0;i<n;i++){ scanf("%d:(%d)",&x,&num); while(num--){ scanf("%d",&y); G[x].push_back(y); G[y].push_back(x); } } memset(vis,0,sizeof(vis)); dfs(0); printf("%d\n",min(dp[0][0],dp[0][1])); } return 0; }
相关文章推荐
- 【树形dp】UVALive 2038 Strategic game
- UVALive 2038 Strategic game--树形dp
- UVALive 2038 - Strategic game (经典树形DP)
- UVALive 2038 Strategic game (树形DP,4级)
- UVALive 2038 Strategic game (树形DP,4级)
- 简单树形dp uvaLive 4472
- UVALive 3412 Pesky Heroes(树形dp)
- UVALIVE 3346 Perfect Domination on Trees 树形DP
- UVA Live Archive 4015 Cave (树形dp,分组背包)
- UVALive 4015 Caves--树形dp
- UVa 2038 - Strategic game(二分图最小顶点覆盖 or 树形DP)
- UVALive 6619 LIKE vs CANDLE(树形DP)
- UVALive - 4614 Moving to Nuremberg (树形DP)
- UVALive 5002/ lightoj 1382 The Queue(树形DP)
- UVALive - 5088 Alice and Bob's Trip 树形DP
- UVALive - 4614 Moving to Nuremberg (树形DP)
- UVALive 7392 Bundles of Joy【bitset】【类树形DP】【杂题】
- UVALive 6631|Jingle Balls|树形DP
- UVALive 4015 树形dp
- UVALive 6919 A game for kids(树形dp)