Ultraviolet A 1292-Strategic game
A tree is provided. When a node is selected, all the edges connected to it can be overwritten.
Solution: first, the rootless tree is converted into a rootless tree, and 0 is the root.
D [I] [0] indicates the minimum number of nodes required to overwrite the subtree with node I as the root when node I is not selected, d [I] [1] indicates the minimum number of nodes required to overwrite the subtree with node I as the root when node I is selected. If node I is not selected, In order to overwrite all nodes connected to node I, each son must be selected. If node I is selected, each son should select a smaller value. Recurrence by DFS order.
State transition equation:
D [I] [0] = sum {d [u] [1]} (u is the son of I)
D [I] [1] = sum {min {d [u] [0], d [u] [1]} + 1 (u is the son of I)
#include
#include #include
using namespace std;vector
node[1550];int n, DP[1550][2], vis[1550];void init() { for (int i = 0; i <= n; i++) { node[i].clear(); DP[i][0] = DP[i][1] = 0; vis[i] = 0; } int x, y, num; for (int i = 0; i < n; i++) { scanf("%d:(%d)", &x, &num); for (int j = 0; j < num; j++) { scanf("%d", &y); node[x].push_back(y); node[y].push_back(x); } }}void DPS(int root) { vis[root] = 1; int num = node[root].size(); for (int i = 0 ; i < num; i++) { int child = node[root][i]; if (vis[child]) continue; DPS(child); DP[root][0] += DP[child][1]; DP[root][1] += min(DP[child][0], DP[child][1]); } DP[root][1]++;}int main() { while (scanf("%d", &n) != EOF) { init(); DPS(0); printf("%d\n", min(DP[0][0], DP[0][1])); } return 0;}