当前位置: 首页 > news >正文

[HDU] 1054 Strategic Game 入门树形DP

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1054

方法:一个节点选了,则其直接子节点可选可不选,一个节点没有选,则直接子节点必须选,所以建议状态转移方程设

F(P,0)为没有选择p点最少要用多少点,

F(P,1)为选择p点最少要用多少点

      {

      sum({F(S,1)|S是p的直接子节点});P为非叶子

F(P,0)=

      0;P为叶子

      },

      {

      sum({  min(F(S,1),F(S,0)) |S是p的直接子节点 })+1;P为非叶子

F(P,1)=

      1;P为叶子

      }

原来问题的解是min(F(root,1),F(root,0)).

注意这种输入方式如何确定root

代码:

#include <iostream>
#include <queue>
#include<algorithm>
using namespace std;
int n,m,MAX=1600;
bool map[1501][1501];
struct Arc
{
    int vetex;
    Arc* nextArc;
};
struct Node
{
    int x;
    int inDegree;
    Arc* firstArc;
    int layer;
	int noChose;
	int chose;
};
Node nodes[1501];
void createArc(int s,int e)
{
    Arc* arc = (Arc*)malloc(sizeof(Arc));
    arc->vetex=e;
    if(nodes[s].firstArc==NULL)
        arc->nextArc=NULL;
    else
        arc->nextArc=nodes[s].firstArc;
    nodes[s].firstArc = arc;
}
int treeDP[2000][2];
int myMin(int x,int y)
{
    return x>y ? y:x;
}
void treeDp(int root)
{
	Arc* arc = nodes[root].firstArc;
	int v,sonCount=0;
	while(arc!=NULL)
	{
		v=arc->vetex;
		treeDp(v);
		nodes[root].noChose += nodes[v].chose;
		nodes[root].chose += myMin(nodes[v].chose,nodes[v].noChose);
		arc=arc->nextArc;
		sonCount++;
	}
	if(sonCount == 0)
	{
		nodes[root].noChose = 0;  
		nodes[root].chose = 1;
	}
}
void main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(treeDP,0,sizeof(treeDP));
        for(int i=0;i<n;i++)
        {
            nodes[i].inDegree=0;
            nodes[i].firstArc=NULL;
			nodes[i].noChose = 0;
			nodes[i].chose = 1;
        }
        for(int i=0;i<n;i++)
        {
            char str[20];
            cin>>str;
            int number=0,posLeft,count=0;
            for(int i=0;i<::strlen(str);i++)
            {
                if(str[i]==':')
                {
                    int seed =1;
                    for(int k =i-1;k>=0;k--)
                    {
                        number+=(seed*(str[k]-'0'));
                        seed*=10;
                    }
                }
                if(str[i]=='(')
                    posLeft=i;
                if(str[i]==')')
                {
                    int seed =1;
                    for(int k = i-1;k>posLeft;k--)
                    {
                        count+=(seed*(str[k]-'0'));
                        seed*=10;
                    }
                }
            }
            nodes[number].x=number;
            for(int y=0;y<count;y++)
            {
                int son;
                cin>>son;
                nodes[son].x=son;
                nodes[son].inDegree++;
                createArc(number,son);
            }
        }

        for(int i=0;i<n;i++)
        {
            if(nodes[i].inDegree==0)
            {
                nodes[i].layer=0;
                treeDp(i);
				cout<<myMin(nodes[i].noChose,nodes[i].chose)<<endl;
                break;
            }
        } 
    }
}

 感想:入门水题

转载于:https://www.cnblogs.com/kbyd/p/3243182.html

相关文章:

  • JS Invalid Label ,eval错误解决方法
  • A2D JS框架 - DES加密解密 与 Cookie的封装(C#与js互相加密解密)
  • boost库在工作(37)网络UDP服务端之七
  • H面试程序(0):字符串一些常用函数的实现
  • 不容易系列之(4)——考新郎[HDU2049]
  • 正则表达式介绍
  • hdu 1029
  • SQL server经验分享:SQLSERVER 被标记为“可疑”的数据库处理方法
  • 代码自动生成工具MyGeneration之一(程序员必备工具)
  • ASP.NET中利用Split实现对Checkbox的字符串分离放到DataTable里面
  • Git基本操作(add,commit的理解)
  • 怎么编写测试驱动程序
  • 百度地图 - 合并模拟器和真机的静态库文件
  • vb常用命名空间
  • Java解析xml配置文件合成器
  • [译]Python中的类属性与实例属性的区别
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • android图片蒙层
  • CSS3 聊天气泡框以及 inherit、currentColor 关键字
  • Git初体验
  • Hexo+码云+git快速搭建免费的静态Blog
  • HTTP那些事
  • iBatis和MyBatis在使用ResultMap对应关系时的区别
  • iOS 颜色设置看我就够了
  • Javascript编码规范
  • Js基础知识(四) - js运行原理与机制
  • MySQL几个简单SQL的优化
  • Mysql数据库的条件查询语句
  • OSS Web直传 (文件图片)
  • Python socket服务器端、客户端传送信息
  • Python 基础起步 (十) 什么叫函数?
  • SpringBoot 实战 (三) | 配置文件详解
  • Spring框架之我见(三)——IOC、AOP
  • v-if和v-for连用出现的问题
  • webgl (原生)基础入门指南【一】
  • webpack+react项目初体验——记录我的webpack环境配置
  • Web标准制定过程
  • 人脸识别最新开发经验demo
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 入手阿里云新服务器的部署NODE
  • mysql面试题分组并合并列
  • 阿里云IoT边缘计算助力企业零改造实现远程运维 ...
  • 通过调用文摘列表API获取文摘
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • (1)STL算法之遍历容器
  • (11)工业界推荐系统-小红书推荐场景及内部实践【粗排三塔模型】
  • (2)MFC+openGL单文档框架glFrame
  • (2020)Java后端开发----(面试题和笔试题)
  • (第61天)多租户架构(CDB/PDB)
  • (机器学习的矩阵)(向量、矩阵与多元线性回归)
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (一)SpringBoot3---尚硅谷总结
  • (转) Face-Resources
  • (转)mysql使用Navicat 导出和导入数据库
  • .NET 材料检测系统崩溃分析