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 ...
随机推荐
- Android之MainActivity类
一.MainActivity: 1.每个种语言都有一个程序入库(如:C#main函数),而Android程序的入口就是Mani Actiivty函数. 2.Activity是Android的核心类(a ...
- 涨知识:equals 和 == 你真的了解吗?
基本概念 ==是运算符,比较的是两个变量是否相等: equals()是Object方法,用于比较两个对象是否相等 看一下源码: public boolean equals(Object anObjec ...
- 八(第一篇)、主体结构元素——article元素、section元素
article元素 article元素代表文档.页面或应用程序中独立的.完整的.可以独自被外部引用的内容. 他可以是一篇博客或者报刊中的文章,一篇轮胎帖子.一段用户评论或独立的插件,或其他任何独立的插 ...
- [python,2018-03-06] python中的继承顺序
python 支持多继承,但对与经典类和新式类来说,多继承查找的顺序是不一样的. 经典类: 新式类 class P1: def foo(self): ...
- 推特算法,分布式ID
package casclient_demo1.util; import java.lang.management.ManagementFactory; import java.net.InetAdd ...
- OpenGL中投影矩阵基础知识
投影矩阵元素Projection Matrix 投影矩阵构建: 当f趋向于正无穷时: 一个重要的事实是,当f趋于正无穷时,在剪裁空间中点的z坐标跟w坐标相等.计算方法如下: 经过透视除法后,z坐标变为 ...
- leetcode1
public class Solution { public int[] TwoSum(int[] nums, int target) { ]; ; i < nums.Length; i++) ...
- leetcode104
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- leetcode152
class Solution { public: int maxProduct(vector<int>& nums) { if(nums.empty()) ; ) ]; ]; // ...
- E0264 Unable to execute '"/usr/bin/codesign" ...'
E0264 Unable to execute '"/usr/bin/codesign" ...' http://docwiki.embarcadero.com/RADStudio ...