Ural 1018 Binary Apple Tree
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1018
Dynamic Programming. 首先要根据input建立树形结构,然后在用DP来寻找最佳结果。dp[i][j]表示node i的子树上最多保存j个分支的最佳结果。代码如下:
- #include <iostream>
- #include <math.h>
- #include <stdio.h>
- #include <cstdio>
- #include <algorithm>
- #include <string.h>
- #include <string>
- #include <sstream>
- #include <cstring>
- #include <queue>
- #include <vector>
- #include <functional>
- #include <cmath>
- #include <set>
- #define SCF(a) scanf("%d", &a)
- #define IN(a) cin>>a
- #define FOR(i, a, b) for(int i=a;i<b;i++)
- #define Infinity 9999999
- typedef long long Int;
- using namespace std;
- int dp[][]; //row -> node id, col -> branches preserved.
- struct link {
- int parent;
- int child;
- int weight;
- };
- struct tree {
- int parent;
- tree *left, *right;
- int leftw, rightw;
- };
- int N, Q;
- link links[];
- bool visited[];
- tree * construct(int pid)
- {
- tree *t = new tree;
- t->parent = pid;
- int num = ;
- bool found = false;
- FOR(i, , N - )
- {
- if (visited[i] == false && (links[i].parent == pid || links[i].child==pid))
- {
- visited[i] = true;
- if (num == )
- {
- t->leftw = links[i].weight;
- if(links[i].parent==pid)
- t->left = construct(links[i].child);
- else
- t->left = construct(links[i].parent);
- num++;
- }
- else if (num == )
- {
- t->rightw = links[i].weight;
- if(links[i].parent==pid)
- t->right = construct(links[i].child);
- else
- t->right = construct(links[i].parent);
- found = true;
- break;
- }
- }
- }
- if (found == false)
- {
- t->left = NULL;
- t->right = NULL;
- }
- return t;
- }
- int calcuTree(tree *t, int R)
- {
- if (t == NULL)
- return ;
- int id = t->parent;
- if (dp[id][R] == -)
- {
- int maxLeft = ;
- for (int leftR = ; leftR <= R; leftR++)
- {
- int cleft = ;
- int rightR = R - leftR;
- if (leftR > )
- {
- cleft += t->leftw;
- cleft += calcuTree(t->left, leftR - );
- }
- if (rightR > )
- {
- cleft += t->rightw;
- cleft += calcuTree(t->right, rightR - );
- }
- if (cleft > maxLeft)
- maxLeft = cleft;
- }
- dp[id][R] = maxLeft;
- }
- return dp[id][R];
- }
- int main()
- {
- while (scanf("%d %d\n", &N, &Q) != EOF)
- {
- FOR(i, , N - )
- {
- int p, c, w;
- scanf("%d %d %d", &links[i].parent, &links[i].child, &links[i].weight);
- }
- FOR(i, , N - )
- visited[i] = false;
- tree *t = new tree;
- t = construct();
- FOR(i, , N + )
- {
- FOR(j, , Q + )
- {
- dp[i][j] = -;
- }
- }
- FOR(i, , N + )
- {
- dp[i][] = ;
- }
- int ans = calcuTree(t, Q);
- printf("%d\n", ans);
- }
- return ;
- }
Ural 1018 Binary Apple Tree的更多相关文章
- CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)
CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...
- URAL 1018 Binary Apple Tree(树DP)
Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a bina ...
- ural 1018 Binary Apple Tree(树形dp | 经典)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- timus 1018. Binary Apple Tree
1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...
- BNUOJ 13358 Binary Apple Tree
Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Orig ...
- 【URAL 1018】Binary Apple Tree
http://vjudge.net/problem/17662 loli蜜汁(面向高一)树形dp水题 #include<cstdio> #include<cstring> #i ...
- URAL1018 Binary Apple Tree(树形DP)
题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k ...
- URAL1018 Binary Apple Tree(树dp)
组队赛的时候的一道题,那个时候想了一下感觉dp不怎么好写呀,现在写了出来,交上去过了,但是我觉得我还是应该WA的呀,因为总感觉dp的不对. #pragma warning(disable:4996) ...
- URAL1018. Binary Apple Tree
链接 简单树形DP #include <iostream> #include<cstdio> #include<cstring> #include<algor ...
随机推荐
- git比较两个分支的文件的差异
Git diff branch1 branch2 --stat //显示出所有有差异的文件列表 Git diff branch1 branch2 文件名(带路径) //显示指定文件的详细差异 ...
- VS在.NETFramework升级时遇到类库冲突如何解决
相信大家在开发环境中随着程序的不断升级,很多时间需要升级. NETFramework版本.今天项目中遇到的问题是从. NETFramework4.0升级到4.5时提示 Entityframework. ...
- 普通PC机支持内存128G,单条32G内存
以前,不管是英特尔还是AMD的消费级平台支持内存容量大多都是64GB,这一现状被英特尔公司去年推出的第九代酷睿而改变.第九代酷睿最大支持128GB内存,虽然只是简单的提高了内存容量,对大多数电脑用户而 ...
- (转)适用微信小程序的table表格(带隔行变色)
原文地址 table.wxml <view class="table"> <view class="tr bg-w"> <view ...
- JVM内存区域详解(Eden Space、Survivor Space、Old Gen、Code Cache和Perm Gen)
JVM区域总体分两类,heap区和非heap区.heap区又分为: Eden Space(伊甸园). Survivor Space(幸存者区). Old Gen(老年代). 非heap区又分: Cod ...
- 学习excel的使用技巧三快捷键和思路
快捷键 CRTL+回车 是多行执行 思路 关于公式 在空白出 写= 即开始写公式 excel第一行 就是行标 比如 A1 就是excel 表格中第一个 比如来个乘法 =A1*12+b1*13 求和更简 ...
- 学习:D3
http://www.ourd3js.com/wordpress/?p=196 http://www.ourd3js.com/demo/rm/R-9.2/force.html 力导向图(那个可以拖拽的 ...
- OpenGL中投影矩阵基础知识
投影矩阵元素Projection Matrix 投影矩阵构建: 当f趋向于正无穷时: 一个重要的事实是,当f趋于正无穷时,在剪裁空间中点的z坐标跟w坐标相等.计算方法如下: 经过透视除法后,z坐标变为 ...
- 多进程模块 multiprocessing
由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程. multiprocessing包是Python中的多进程 ...
- HTML5-canvas1.0
HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.<canvas> 标签只是图形容器,您必须使用脚本来绘制图形.你可以通过多种方 ...