zoukankan      html  css  js  c++  java
  • [POJ 1463] Strategic game

    问题描述

    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.

    For example for the tree:

    the solution is one soldier ( at the node 1).

    输入格式

    The input 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_identifiernumber_of_roads
      or
      node_identifier:(0)

    The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.

    输出格式

    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

    样例输入

    4
    0:(1) 1
    1:(2) 2 3
    2:(0)
    3:(0)
    5
    3:(3) 1 4 2
    1:(1) 0
    2:(0)
    0:(0)
    4:(0)

    样例输出

    1
    2

    题目大意

    给你一棵树,要求涂黑最少的点,使每个点都至少与一个黑点相邻。

    解析

    对于任意一个点,它若与一个黑点相邻,这个黑点只可能是它的儿子或者父亲(显然......)。所以树形动规的初步模型就有了。我们需要记录的状态就是这个点是否被他的父亲或儿子覆盖。设(f[i][0/1])表示(i)点没被覆盖(0)或被覆盖(1)时以(i)为根的子树所用的最少点。那么对于节点a的父节点b,如果b没有被覆盖,那么a就必须被子节点c覆盖;如果b被覆盖了就随便。由此,状态转移方程如下:

    [egin{cases} f[u][0]+=f[v][1]\ f[u][1]=min(f[v][1],f[v][0]) end{cases} ]

    再借助树形动规的递归结构就可以很好地解决本题了。

    代码

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #define N 1502
    using namespace std;
    int head[N],ver[N*2],nxt[N*2],l;
    int n,i,j,f[N][2];
    void insert(int x,int y)
    {
    	l++;
    	ver[l]=y;
    	nxt[l]=head[x];
    	head[x]=l;
    }
    void dp(int x,int pre)
    {
    	f[x][1]=1;
    	for(int i=head[x];i;i=nxt[i]){
    		int y=ver[i];
    		if(y!=pre){
    			dp(y,x);
    			f[x][1]+=min(f[y][0],f[y][1]);
    			f[x][0]+=f[y][1];
    		}
    	}
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF){
    	    memset(head,0,sizeof(head));
    		memset(f,0,sizeof(f));
    		l=0;
    		for(i=1;i<=n;i++){
    			int u,v,w;
    			scanf("%d:(%d)",&u,&w);
    			for(j=1;j<=w;j++){
    				cin>>v;
    				insert(u,v);
    				insert(v,u);
    			}
    		}
    		dp(0,-1);
    		int ans=min(f[0][0],f[0][1]);
    		cout<<ans<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    c#数据结构与算法
    学习资源---.NET
    怎样完全删除sqlserver
    树,森林 二叉树之间转化 原理
    ref 和out 区别
    GridView批量删除记录、全选及弹出确认对话框
    .NET基础 小记--------2013.8.10
    Xml 读写
    同步 异步 区别
    委托学习
  • 原文地址:https://www.cnblogs.com/LSlzf/p/10973219.html
Copyright © 2011-2022 走看看