Ultraviolet A 1292-Strategic game

Source: Internet
Author: User

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;}
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.