poj1463树形dp
来源:互联网 发布:管家婆 远程数据库 编辑:程序博客网 时间:2024/06/05 04:20
题目大意:最少需要多少个点上方士兵,才能观察到所有边。
思路:dp[i][0]表示节点上不放士兵,dp[i][1] 表示节点i放士兵。那么dp[i]均可由子树得到。dp[i][0] = dp[j0][1]+dp[j1][1] + …… , dp[i][1] = min(dp[j0][1] , dp[j0][0] ) + min(dp[j1][0] , dp[j1][1]) + ……因为当前节点放士兵时子节点可放可不放(很重要,刚开始疏忽了)。
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <fstream>#include <algorithm>#include <cmath>#include <queue>#include <stack>#include <vector>#include <map>#include <set>#include <iomanip>using namespace std;//#pragma comment(linker, "/STACK:102400000,102400000")#define maxn 1505#define MOD 1000000007#define mem(a , b) memset(a , b , sizeof(a))#define LL long long#define INF 100000000int n , m;int vis[maxn];vector<int>v[maxn];char str[10];int dp[maxn][2];void dfs(int root){ int up = v[root].size(); if(up == 0) {dp[root][0] = 0 , dp[root][1] = 1; return;} vis[root] = 0; for(int i = 0 ; i < up ; i ++) { if(!vis[v[root][i]]) dfs(v[root][i]); dp[root][0] += dp[v[root][i]][1]; dp[root][1] += min(dp[v[root][i]][0] , dp[v[root][i]][1]); }}int main(){ while(scanf("%d" , &n) != EOF) { mem(dp , 0);mem(vis , 0); for(int i = 0 ; i <= n ; i ++) v[i].clear() , dp[i][1] = 1; for(int i = 0 ; i < n ; i ++) { scanf("%s" , str); int up = 0 , id = 0 , e , tmp = 0; while(str[id] != ':') tmp = tmp * 10 + (str[id++] - '0') ; id ++;id++; while(str[id] != ')') up = up * 10 + (str[id++] - '0'); // cout << up << " " << tmp << endl; for(int j = 0 ; j < up ; j ++) { int u = tmp; scanf("%d" , &e); if(u > e) swap(u , e); v[u].push_back(e); // cout << e << endl; } } //cout << "test" << endl; dfs(0); printf("%d\n" , min(dp[0][0] , dp[0][1])); }}
0 0
- poj1463 树形dp
- 树形dp poj1463
- POJ1463 简单树形DP
- poj1463 树形dp
- poj1463树形dp
- poj1463(树形DP)
- POJ1463->树形DP
- poj1463——树形DP
- POJ1463 Strategic game(树形DP)
- POJ1463 Strategic game(树形DP)
- poj1463 Strategic game 树形dp
- ZOJ1134 POJ1463 HDU1054 Strategic Game, 树形DP
- HDU1054 && POJ1463:Strategic game(树形DP)
- poj1463 Strategic game(树形dp)
- poj1463--hdu1054--Strategic Game(树形DP练习4)
- #POJ1463#Strategic Game(贪心 or Hungary or 树形DP)
- 【贪心】【树形DP】[POJ1463][HDU1054]Strategic game 战略游戏
- poj1463树形动归
- Solr索引库l配置文件schema.xm要点
- getopt和getopt_long函数
- EasyDarwin开源流媒体服务器高性能设计之无锁队列
- JavaScript之面向对象的继承浅析2
- 最小生成树-克鲁斯卡尔方法(Kruskal)
- poj1463树形dp
- 编译android源码3---ubuntu安装jdk6
- 最详细的制作正式版10.11 OS X El Capitan 安装U盘的方法
- android webview 访问https页面 SslError 处理
- 【Linux 基础篇】之 rsync
- [BZOJ3680]吊打XXX
- python第五天
- Mahout实现协同过滤的基本概念理解
- View源码学习笔记