Strategic game strategy game

POJ

Meaning of the questions: There is a city, there are many roads to link the entire city looks like a tree as a whole need to place as few soldiers to defend all sides of the tree soldiers can only be placed on the node, but.. wherewith all the nodes adjacent sides. It requires at least need to place the number of soldiers?

Analysis: The maximum independent set of the tree, the tree is provided to do DP. \ (F [x] [1/0] \) denotes the subtree rooted at the node x, the node x ck / hold a minimum of soldiers place the number of soldiers.

Let y is a child node x, then,

\ (F [x] [0] = F + [y] [. 1] \) , x nodes hold soldiers, all child nodes of the x, y to be placed soldiers.

\ (F [X] [. 1] + = min (F [y] [0], F [y] [. 1]) \) , X node put soldiers, its child node y can be put can hold, taking the most excellent value.

Initialization: \ (F [X] [0] = 0, F [X] [. 1] =. 1 \)

Target: \ (min (F [the root] [0], F [the root] [. 1]) \)

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int read(){
   int s=0,w=1;char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
   return s*w;
}
const int N=1505;
struct ppx{
    int num,son[N];
}a[N];
int bj[N],f[N][2];
inline void dp(int x){
    f[x][0]=0;f[x][1]=1;//初始化
    for(int i=1;i<=a[x].num;i++){
        int y=a[x].son[i];
        dp(y);//树形DP一般都是先往下走,再更新
        f[x][0]+=f[y][1];
        f[x][1]+=min(f[y][0],f[y][1]);
    }
}
int main(){
    int n;
    while(~(scanf("%d",&n))){
        memset(bj,0,sizeof(bj));//初始化
        for(int i=1,x;i<=n;i++){
            x=read();a[x].num=read();
            for(int j=1,y;j<=a[x].num;j++){
                y=read();a[x].son[j]=y;
                bj[y]++;
            }
        }
        int root=0;while(bj[root])root++;//找根
        dp(root);
        printf("%d\n",min(f[root][0],f[root][1]));
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/PPXppx/p/10989868.html