zoukankan      html  css  js  c++  java
  • hdu 1054 Strategic Game(tree dp)

    Strategic Game

    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3806    Accepted Submission(s): 1672

    Problem Description
    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.

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

    The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.

    For example for the tree:



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

    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 table:
     
    Sample Input
    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)
     
    Sample Output
    1 2
     
    Source
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    #define N 2010
    int Min(int i,int j){return i<j?i:j;}
    struct node{
    	node *l,*r;//l-son,r-brother
    	int f[2];
    	void init(){
    		l=r=NULL;f[0]=f[1]=0;
    	}
    }t[N],*rt;
    int r[N];
    void dfs(node *x){
    	//遍历x儿子
    	node *l=x->l;
    	while(l){
    		dfs(l);
    		//放在x上
    		x->f[1]+=Min(l->f[0],l->f[1]);
    		//x不放
    		x->f[0]+=l->f[1];
    		l=l->r;
    	}
    }
    int main(){
    	int i,j,k,x,y,n,m;
    	while(~scanf("%d",&n)){
    		m=0;
    		memset(r,-1,sizeof(r));
    		while(n--){
    			scanf("%d:(%d)",&i,&k);
    			if(r[i]==-1){r[i]=m;t[m++].init();}
    			x=r[i];
    			while(k--){//i->l=j
    				scanf("%d",&j);
    				if(r[j]==-1){r[j]=m;t[m++].init();}
    				y=r[j];
    				t[y].f[1]=1;
    				t[y].r=t[x].l;
    				t[x].l=&t[y];
    			}
    		}
    		for(i=0;;i++)
    			if(t[r[i]].f[1]==0){
    				rt=&t[r[i]];
    				rt->f[1]=1;
    				break;
    			}
    		dfs(rt);
    		printf("%d
    ",Min(rt->f[0],rt->f[1]));
    	}
    return 0;
    }

  • 相关阅读:
    vue 手动挂载 $amount()
    Redis 主从配置
    DMA分区管理
    C# 构造函数里的base和this的区别
    SQL Server 数据库性能优化
    TCP和UDP的优缺点及区别
    Django框架初步应用简述
    前端vue框架应用雏形
    接口mock之moco
    python进阶(九)~~~协程、进程池、线程/进程/协程对比
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3238839.html
Copyright © 2011-2022 走看看