题目链接: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的更多相关文章

  1. 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 ...

  2. 树形DP URAL 1039 Anniversary Party

    题目传送门 /* 题意:上司在,员工不在,反之不一定.每一个人有一个权值,问权值和最大多少. 树形DP:把上司和员工的关系看成根节点和子节点的关系,两者有状态转移方程: dp[rt][0] += ma ...

  3. ural 1039 树dp

    http://acm.timus.ru/problem.aspx?space=1&num=1039 1039. Anniversary Party Time limit: 0.5 second ...

  4. URAL 1776 Anniversary Firework (概率,区间DP)

    坑,一开始以为,分成两半的时候去最大那个就行了, 实际上这样是不对的,因为有可能出现小的一半的时间比大的要长, 因为还和等待次数有关,且转移的时候需要用到次数更小的状态, 所以状态定义为二维,dp[i ...

  5. 【ACM/ICPC2013】树形动态规划专题

    前言:按照计划,昨天应该是完成树形DP7题和二分图.最大流基础专题,但是由于我智商实在拙计,一直在理解树形DP的思想,所以第二个专题只能顺延到今天了.但是昨天把树形DP弄了个5成懂我是很高兴的!下面我 ...

  6. URAL 1776 C - Anniversary Firework DP

    C - Anniversary FireworkTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/c ...

  7. POJ 2342 Anniversary party(树形dp)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7230   Accepted: 4162 ...

  8. HDOJ 1520 Anniversary party

    树形DP....在树上做DP....不应该是猴子干的事吗?  Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

  9. hdu1520 树形dp Anniversary party

    A - Anniversary party Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

随机推荐

  1. bananapi+OLED做的一个打地鼠游戏,c语言编程

    说明一下:BPI是对拍死的BPI的计数,对应最终的成绩RANK是难度 数值越低难度越高 每当打死10个BPI以后就会减一即难度高一级 默认初始化RANK等于15 DIE是存在的BPI数量,一旦数量大于 ...

  2. C++Primer第五版——习题答案详解(八)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第9章 顺序容器 练习9.1 a.list,需要按字典序插入,可能插入位置在中间 b.d ...

  3. 理解Linux系统负荷load average

    理解Linux系统负荷   一.查看系统负荷 如果你的电脑很慢,你或许想查看一下,它的工作量是否太大了. 在Linux系统中,我们一般使用uptime命令查看(w命令和top命令也行).(另外,它们在 ...

  4. solr搜索

    安装过程: 原料:solr-4.10.3.tgz.tgz 1.1.1 安装步骤 单独一台虚拟机先全部删除:根目录:rm * -rf             cd /usr/local   \  rm ...

  5. leetcode160

    /** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...

  6. Docker 清理命令

    原文地址http://www.runoob.com/w3cnote/docker-clear-command.html,这里仅作为记录,便于以后查阅 查看正在运行的容器(Container) dock ...

  7. CheckBox获取一组及全选

    获取一组CheckBox: jQuery: $(function () { $("input[name=names]").click(function () { //获得所有的na ...

  8. linux下redis4.0.2集群部署(利用Ruby脚本命令)

    一.原生命令方式和Ruby脚本方式区别 利用Ruby脚本部署和用原生命令部署,节点准备的步骤都是一样的,节点启动后的握手,以及主从.槽分配,利用Ruby脚本一步就能完成,利用原生命令需要一步一步地执行 ...

  9. 如何将 jar 包导入Maven 本地仓库

    案例:oracle jar包由于在maven 远程仓库中找不到,需要先将oracle jar 文件下载到本地,然后导入maven本地仓库,就可以通过 pom 进行依赖 例如:下载后的 jar 地址 D ...

  10. linux查询公网ip

    查询公网ip[出口IP] 1.curl icanhazip.com 2.curl ipecho.net/plain 3.curl www.trackip.net/i