Minimum Vertex Cover the tree

Trees Minimum Vertex Cover: consumption takes on a node can cover its own and all adjacent nodes with it, find the minimum cost tree cover all nodes.

1077. Palace guard

Taiping Wang Shizi incident, Lu Xiaofeng became the Chair of the emperor's imperial guards of a product.
Distribution of the individual palace palace, showing the shape of a tree, the tree can be considered palace junction, if there is a road between two directly connected to the palace, the road tree considered an edge.
Known, in a palace guard guards not only able to observe the situation of the palace, the palace also observed that the presence of other road directly connected with the palace situation.
Ouchi guard guarded, three steps Kong, Buyi post every palace was to be guarded around the clock, arrangement fees detention center to be different in different palaces.
But lack of funds in the hands of Lu Xiaofeng, in any case are not left behind guards placed at each palace.
Lu Xiaofeng help arrange guards, under the premise of the entire palace guard, so that funds spent at least.
Input format
input data described in a tree, as described below:
The first line n, the number of nodes in the tree representation.
The second row to the n + 1 lines, each line describes information of each node palace, as follows: The reference palace node i, required the provision palace guards disposed k, the sub-node number of nodes m, Next number m, respectively, reference numeral r1 m child nodes of this node, r2, ..., rm.
For a tree of n nodes, the node between the reference numeral 1 to n, and reference numerals will not be repeated.
Output format
output an integer representing the minimum requirements.
Data range
1≤n≤1500
Input Sample:
. 6
. 1 2. 3 30. 3. 4
2 16 2. 5. 6
. 3. 5 0
. 4 0. 4
. 5. 11 0
. 6. 5 0
Output Sample:
25
Sample Explanation:
arrange the guard 2,3,4 node can be observed all the palace, the minimum required funding for 16 + 5 + 4 = 25.

Each point may be selected son, or their parent node coverage (at least covered with a unique minor), can be f [u] [0] son node prevents guards, f [u] [1] represents himself a guards, f [u] [2] represents the father put guards
f [u] [0] by one child node f [v] [1] and the other child nodes f [v] [0] and [1] is the minimum value ( no [2] because it can not rely on the parent node), traversing through all possible child nodes, takes a minimum value. I.e., as long as put guards node of a child node may be covered
f [u] [1] all instances by the child nodes transferred from, since the node can cover all child nodes
f [u] [2] from the child node f [v] [0] and f [v] [1] transferred from the minimum value, because it can not cover the child node

#include<bits/stdc++.h>
using namespace std;
const int N=1510;
int h[N],idx,a[N],f[N][3];  //0儿子放 ,1自己放 ,2父亲放
struct eg{
    int v,nex;
}e[N*2];
void add(int u,int v){
    e[idx]={v,h[u]};
    h[u]=idx++;
}
void dfs(int u,int pre){
    f[u][1]=a[u];
    //f[u][0]
    int res=0;
    for(int i=h[u];~i;i=e[i].nex){
        int v=e[i].v;
        if(v==pre) continue;
        dfs(v,u);
        f[u][1]+=min(f[v][0],min(f[v][1],f[v][2]));
        f[u][2]+=min(f[v][1],f[v][0]);
        res+=min(f[v][0],f[v][1]);
    }
    int ans=0x3f3f3f3f;
    for(int i=h[u];~i;i=e[i].nex){
        int v=e[i].v;
        if(f[v][0]>=f[v][1]){//儿子自己放
            ans=min(ans,res);
        }
        else { //儿子的儿子放
            ans=min(ans,res-f[v][0]+f[v][1]);
        }
        //ans=min(ans,res-min(f[v][0],f[v][1])+f[v][1]);
    }
    f[u][0]=ans;
}
int main(){
    int n;
    memset(h,-1,sizeof h);
    cin>>n;
    for(int i=1;i<=n;++i){
        int u,k,v;
        scanf("%d",&u);
        scanf("%d%d",&a[u],&k);
        while(k--){
            scanf("%d",&v);
            add(u,v);
            add(v,u);
        }
    }
    if(n==1) cout<<a[1]<<endl;
    else {
        dfs(1,0);
        cout<<min(f[1][0],f[1][1])<<endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/jjl0229/p/12654796.html