UVALive 2038 - Strategic game (经典树形DP)
来源:互联网 发布:天气软件 编辑:程序博客网 时间:2024/04/29 12:41
[Submit] [Go Back] [Status]
Description
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?
Your program should find the minimum number of soldiers that Bob has to put for a given tree.
For example for the tree:
the solution is one soldier (at the node 1).
Input
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n ≤ 1500). Every edge appears only once in the input data.the number of nodes the description of each node in the following format: node_identifier:(number_of_roads) node_identifier1 node_identifier2 … node_identifiernumber_of_roads
or
node_identifier:(0)
Output
Sample Input
40:(1) 11:(2) 2 32:(0)3:(0)53:(3) 1 4 21:(1) 02:(0)0:(0)4:(0)
Sample Output
12
[Submit] [Go Back] [Status]
题意:
给定一棵树,选择尽量少的节点,使得每个没有选择的节点至少和一个已选择的节点相邻
更正:
大白上面的翻译就是上面那样,题目的翻译下面这样。
给定一棵树,选择尽量少的节点放置士兵,使得每条边都能被士兵照顾到。
如果是大白翻译那样,这样的做法就是错的,比如
14
0:(3) 1 2 3
1:(3) 4 5 6
2:(1) 7
3:(3) 8 9 10
4:(0)
5:(0)
6:(0)
7:(3) 11 12 13
8:(0)
9:(0)
10:(0)
11:(0)
12:(0)
13:(0)
这个画出来就可以看到
按照大白的题意答案应该是3
而根据题面的答案应该是4
经典树形DP
dp[i][j] 表示i节点有没有选择j=0没有选择j=1选择
#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#define oform1 "%I64d"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#define oform1 "%lld"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define P64I1(a) printf(oform1, (a))#define REP(i, n) for(int (i)=0; (i)<n; (i)++)#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 10e-9;const double PI = (4.0*atan(1.0));const int maxn = 1500 + 20;vector<int> G[maxn];int dp[maxn][2];int f(int u, int sel, int fa) { if(dp[u][sel] != -1) return dp[u][sel]; int & ret = dp[u][sel]; ret = sel; for(int i=0; i<G[u].size(); i++) { int v = G[u][i]; if(v == fa) continue; if(sel) ret += min(f(v, 0, u), f(v, 1, u)); else ret += f(v, 1, u); } return ret;}int main() { int n; while(scanf("%d", &n) != EOF) { for(int i=0; i<n; i++) G[i].clear(); for(int i=0; i<n; i++) { int u, m; scanf("%d:(%d)", &u, &m); for(int i=0; i<m; i++) { int v; scanf("%d", &v); G[u].push_back(v); G[v].push_back(u); } } memset(dp, -1, sizeof(dp)); int c0 = f(0, 0, -1); int c1 = f(0, 1, -1); int ans = min(c0, c1); printf("%d\n", ans); } return 0;}
- UVALive 2038 - Strategic game (经典树形DP)
- 【树形dp】UVALive 2038 Strategic game
- UVALive 2038 Strategic game--树形dp
- UVALive 2038 Strategic game (树形DP,4级)
- 树形dp Strategic game
- Strategic game(树形DP)
- (树形DP) Strategic game(P1463)
- POJ Strategic game(树形DP)
- UVA 1292 - Strategic game(树形dp)
- uva 1292 - Strategic game(树形dp)
- hdu - 1054 - Strategic Game(树形dp)
- UVA 1292-Strategic game(树形DP)
- HDU 1054 Strategic Game (树形DP)
- poj 1463 Strategic game (树形DP)
- poj1463 Strategic game(树形dp)
- UVALive 2038 Strategic game
- UVALive 2038 Strategic game
- UVALive 2038 Strategic game
- iOS ASI http 框架详解
- 高并发的epoll+线程池,epoll在线程池内
- JQuery判断数组中是否包含某个元素$.inArray("js", arr);
- Java IO流学习笔记
- 重构之一起点
- UVALive 2038 - Strategic game (经典树形DP)
- 枚举进程
- matlab 黄金分割算法
- 【Linux】Linux下的多线程编程
- T-code
- 解决error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppC
- BeanUtils的转换器
- 终于在kali linux中安装好了快盘1402版本。
- python爬虫小程序