HDU 1054 Strategic Game
来源:互联网 发布:java中源代码什么意思 编辑:程序博客网 时间:2024/06/05 11:25
Problem 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.
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_identifier
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.
For example for the tree:
the solution is one soldier ( at the node 1).
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). An example is given in the following table:
Your program should find the minimum number of soldiers that Bob has to put for a given tree.
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_identifier
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.
For example for the tree:
the solution is one soldier ( at the node 1).
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). An example is given in the following table:
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
树形dp或者最大匹配。
#include<iostream>#include<cstdio>#include<vector>#include<algorithm>using namespace std;const int maxn = 1505;vector<int> tree[maxn];int n, x, y, z, dp[maxn][2];void work(int x, int fa){dp[x][0] = 0; dp[x][1] = 1;for (int i = 0; i < tree[x].size();i++)if (tree[x][i] != fa){int y = tree[x][i];work(y, x);dp[x][1] += min(dp[y][0], dp[y][1]);dp[x][0] += dp[y][1];}}int main(){while (~scanf("%d", &n)){for (int i = 0; i < n; i++) tree[i].clear();for (int i = 0; i < n; i++){scanf("%d:(%d)", &x, &z);while (z--){scanf("%d", &y);tree[x].push_back(y);tree[y].push_back(x);}}work(0, 0);cout << min(dp[0][0], dp[0][1]) << endl;}}网上能找到的树形dp都是这么做的,但是其实按题意来说,这样写应该是有问题的。0:(3) 1 2 3
1:(1) 4
2:(1) 5
3:(1) 7
4:(0)
5:(1) 6
6:(0)
7:(1) 8
8:(0)
上面的结果应该是3 ,但是结果却是4。
暂时不知该怎么解决,再说吧。
0 0
- hdu 1054 Strategic Game
- HDU 1054 Strategic Game
- hdu 1054 Strategic Game
- hdu 1054 Strategic Game
- Hdu 1054 Strategic Game
- hdu 1054 Strategic Game
- HDU - 1054 Strategic Game
- hdu 1054 Strategic Game
- hdu-1054-Strategic Game
- HDU 1054 Strategic Game
- HDU 1054 Strategic Game
- HDU 1054 Strategic Game
- HDU 1054:Strategic Game
- HDU 1054 Strategic Game
- HDU 1054 Strategic Game
- HDU 1054Strategic Game 贪心
- hdu/hdoj 1054 Strategic Game
- hdu 1054 Strategic Game (二分匹配)
- XSS攻击与防御
- too much to learn...
- 中科院词性标注标记集
- 接口与抽象类
- PE结构各字段偏移参考
- HDU 1054 Strategic Game
- C++中的单例模式
- 【bzoj3064】【Tyvj 1518 CPU监控】
- pat1013
- 工厂模式
- 指针函数与函数指针的区别
- hadoop--自动化脚本-- 自动给奴隶机器发送hadoop安装包
- atitit.新增编辑功能 跟orm的实现 attilax p31
- 锁与隔离级别的关系