Ural 1039 Anniversary Party
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1039
Dynamic Programming. 建立树形结构,每个employee有两个选择,去或者不去。supervisor的选择影响着sub-tree的选择。代码如下:
#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; struct tree {
int parent;
vector<int> children;
int size = ;
};
int N;
tree * trees[];
int dp[][]; //i -> employee id, j -> attend 1 or not 0
int conviviality[]; int calcuTree(int parent, int choice) //choice 1: attend or not, choice 0: cannot atted
{
if (dp[parent][choice] != Infinity)
return dp[parent][choice]; int maxCon = ;
int maxConDGo = ;
int maxConGo = conviviality[parent];
//this employee don't go, its child has 2 choices
for (int i = ; i < trees[parent]->size; i++)
{
int child = trees[parent]->children[i];
if (dp[child][] == Infinity)
dp[child][] = calcuTree(child, );
if (dp[child][] == Infinity)
dp[child][] = calcuTree(child, );
maxConDGo += max(dp[child][], dp[child][]);
} //this employee go, its child has only one choice.
for (int i = ; i < trees[parent]->size; i++)
{
int child = trees[parent]->children[i];
if (dp[child][] == Infinity)
dp[child][] = calcuTree(child, );
maxConGo += dp[child][];
} if (choice == )
maxCon = max(maxConGo, maxConDGo);
else
maxCon = maxConDGo; return maxCon;
} int main()
{
while (SCF(N) != EOF)
{
FOR(i, , N + )
SCF(conviviality[i]);
int L, K;
int index = ;
FOR(i, , N + )
{
trees[i] = new tree;
trees[i]->parent = i;
} bool *root = new bool[N + ];
FOR(i, , N + )
root[i] = true; while (scanf("%d %d", &L, &K))
{
if (L == && K == )
break;
trees[K]->children.push_back(L);
trees[K]->size += ;
root[L] = false;
} FOR(i, , N + )
{
dp[i][] = dp[i][] = Infinity;
} int president = ;
FOR(i, , N + )
{
if (root[i])
{
president = i;
break;
}
} FOR(i, , N + )
{
dp[i][] = calcuTree(i, );
dp[i][] = calcuTree(i, );
} int maxAns = max(dp[president][], dp[president][]);
printf("%d\n", maxAns);
}
return ;
}
Ural 1039 Anniversary Party的更多相关文章
- POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)
POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...
- 树形DP URAL 1039 Anniversary Party
题目传送门 /* 题意:上司在,员工不在,反之不一定.每一个人有一个权值,问权值和最大多少. 树形DP:把上司和员工的关系看成根节点和子节点的关系,两者有状态转移方程: dp[rt][0] += ma ...
- ural 1039 树dp
http://acm.timus.ru/problem.aspx?space=1&num=1039 1039. Anniversary Party Time limit: 0.5 second ...
- URAL 1776 Anniversary Firework (概率,区间DP)
坑,一开始以为,分成两半的时候去最大那个就行了, 实际上这样是不对的,因为有可能出现小的一半的时间比大的要长, 因为还和等待次数有关,且转移的时候需要用到次数更小的状态, 所以状态定义为二维,dp[i ...
- 【ACM/ICPC2013】树形动态规划专题
前言:按照计划,昨天应该是完成树形DP7题和二分图.最大流基础专题,但是由于我智商实在拙计,一直在理解树形DP的思想,所以第二个专题只能顺延到今天了.但是昨天把树形DP弄了个5成懂我是很高兴的!下面我 ...
- URAL 1776 C - Anniversary Firework DP
C - Anniversary FireworkTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/c ...
- POJ 2342 Anniversary party(树形dp)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7230 Accepted: 4162 ...
- HDOJ 1520 Anniversary party
树形DP....在树上做DP....不应该是猴子干的事吗? Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- hdu1520 树形dp Anniversary party
A - Anniversary party Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
随机推荐
- Optaplanner - 从探究示例中的hello world,初步认识规划引擎的运行步骤。
上一篇我们成功以把Opotaplanner规划引擎下载回来,并把它的示例运行起来,简单解析了一下它的Cloud balance示例.这一篇我们这些示例的源代码导入到Eclipse中,看看它在后台是怎么 ...
- 图的遍历——DFS和BFS模板(一般的图)
关于图的遍历,通常有深度优先搜索(DFS)和广度优先搜索(BFS),本文结合一般的图结构(邻接矩阵和邻接表),给出两种遍历算法的模板 1.深度优先搜索(DFS) #include<iostrea ...
- Linux的命令技巧
一.使用apt-get installl 方法安装的库或者程序一般的路径如下 1.下载的软件存放位置 /var/cache/apt/archives 2.安装后软件默认位置 /usr ...
- composer在phpstorm中安装代码库
E:\php\PHPTutorial\WWW\kmmhtt>composer install composer 安装地址 :https://getcomposer.org/download/
- [持续交付实践] pipeline使用:项目样例
项目说明 本文将以一个微服务项目的具体pipeline样例进行脚本编写说明.一条完整的pipeline交付流水线通常会包括代码获取.单元测试.静态检查.打包部署.接口层测试.UI层测试.性能专项测试( ...
- yum梳理
- leetcode200
深度优先搜索,每次遇到1,则岛的数量+1,从这个1开始找到所有相连的1,将其改为0. public class Solution { private void dfsSearch(char[,] gr ...
- <记录> curl 封装函数
1. POST请求 参数1 : 请求地址 参数2 : 数组形式的参数 /** * @param string $url post请求地址 * @param array $params * @retur ...
- web和app的简单测试区别和工具介绍
首先说一下我对Web自动化测试与CS自动化测试的认识.从宏观对比都是通过脚本自动化完成功能的验证,区别不大.Web测试更为显著的浏览器兼容性.安全,以及与Web技术相关的表单测试.链接测试等,其实都是 ...
- POI richText和html的转换案例
private XSSFRichTextString parseHtmlStrToRichText(String htmlStr) { Document document = parseHtmlStr ...