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 ...
随机推荐
- mysql ibdata1
ibdata1是什么? Mysql ibdata1即Innodb data1缩写,是innodb引擎的表空间,用于存放 数据字典Data dictionary: 只读的表,存储对象的相关信息,如占用 ...
- 廖雪峰Java7处理日期和时间-4最佳实践-最佳实践
jdk提供了2套新旧的API来处理日期和时间. java.util * Date * Calendar java.time(JDK>=1.8) * Localdate * LocalTime * ...
- windows本地eclispe运行linux上hadoop的maperduce程序
继续上一篇博文:hadoop集群的搭建 1.将linux节点上的hadoop安装包从linux上下载下来(你也可以从网上直接下载压缩包,解压后放到自己电脑上) 我的地址是: 2.配置环境变量: HAD ...
- Vue中table合并单元格用法
<table> <tr> <th>地名</th> <th>结果</th> <th>人名</th> < ...
- 2-zookeeper、ha
1.zookeeper ①背景: Zookeeper 最早起源于雅虎研究院的一个研究小组.在当时,研究人员发现,在雅虎内部很多大型系统基本都需要依赖一个类似的系统来进行分布式协调, 但是这些系统往往都 ...
- Oracle 循环查询
select * from sys_department start with departmentid = '0303e461-2454-4d5a-bfea-2cd5a4c064c6' connec ...
- 200. Spring Boot JNDI:在Tomcat中怎么玩JNDI?
[视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源 ...
- 实战ELK(4)Metricbeat 轻量型指标采集器
一.介绍 用于从系统和服务收集指标.从 CPU 到内存,从 Redis 到 Nginx,Metricbeat 能够以一种轻量型的方式,输送各种系统和服务统计数据. 1.系统级监控,更简洁(轻量型指标采 ...
- java.io.UnsupportedEncodingException
启动项目抛错: java.io.UnsupportedEncodingException: 1 at java.lang.StringCoding.decode(StringCoding.java:1 ...
- BBS(第一天)项目之 注册功能实现通过forms验证与 前端ajax请求触发查询数据库判断用户是否存在的功能实现
1.BBS项目之注册功能通过forms验证 from django import forms from blog.models import User from django.contrib.auth ...