UVALive 2038 - Strategic game (经典树形DP)
2014-08-11 15:56
537 查看
Strategic game
[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 input file contains several data sets in text format. Each data set represents a tree with the following description:
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)
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.
Output
The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers).
Sample Input
Sample Output
[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选择
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
[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 input file contains several data sets in text format. Each data set represents a tree with the following description:
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)
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.
Output
The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers).
Sample Input
4 0:(1) 1 1:(2) 2 3 2:(0) 3:(0) 5 3:(3) 1 4 2 1:(1) 0 2:(0) 0:(0) 4:(0)
Sample Output
1 2
[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 WIN typedef __int64 LL; #define iform "%I64d" #define oform "%I64d\n" #define oform1 "%I64d" #else typedef 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,4级)
- UVALive 2038 Strategic game (树形DP,4级)
- 【树形dp】UVALive 2038 Strategic game
- UVALive 2038 Strategic game--树形dp
- UVALive2038(树形dp)
- UVa 2038 - Strategic game(二分图最小顶点覆盖 or 树形DP)
- UVALive4015(树形dp)
- UVA 1292-Strategic game(树形DP)
- UVALive 4015 树形dp
- UVALive - 4614 Moving to Nuremberg (树形DP)
- 2006 Asia - Kaohsiung Perfect Service 树形DP (uvaLive3685) ★
- UVALive 3683 A Scheduling Problem(树形DP)
- UVA 1292 - Strategic game(树形dp)
- UVALive 5088 Alice and Bob's Trip(树形DP)
- LA 2038 Strategic game(二分图最小顶点覆盖 /树形DP)
- hdu 1054 Strategic Game 经典树形DP
- UVA Live Archive 4015 Cave (树形dp,分组背包)
- UVaLive 10859 Placing Lampposts (树形DP)
- uva 1292 - Strategic game(树形dp)
- UVALive - 4015 Caves 树形DP