当前位置:网站首页>树形DP AcWing 285. 没有上司的舞会
树形DP AcWing 285. 没有上司的舞会
2022-07-27 10:35:00 【T_Y_F666】
代码虚拟空间
函数内部空间存储在栈中 C++默认栈空间大小为4M 故开在函数内部容易暴栈
全局变量静态变量存储在堆中
树形DP AcWing 285. 没有上司的舞会
原题链接
算法标签
动态规划 树形DP
思路

状态计算

代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 6005;
int a[N], e[N], h[N], ne[N], idx;
int f[N][2];
bool p[N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
void add(int a, int b){
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
void dfs(int u){
f[u][1]=a[u];
for(int i=h[u]; i!=-1; i=ne[i]){
int j=e[i];
dfs(j);
f[u][1]+=f[j][0];
f[u][0]+=max(f[j][0], f[j][1]);
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read();
rep(i, 1, n+1){
a[i]=read();
}
memset(h, -1, sizeof h);
rep(i, 0, n-1){
int a=read(), b=read();
add(b, a);
p[a]=true;
}
int root=1;
// 查询根节点
while(p[root]){
root++;
}
dfs(root);
printf("%lld", max(f[root][0], f[root][1]));
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Knapsack model acwing 423. Picking herbs
- Non progressive phenomena of entropy and morphology
- Local and overall differences between emergence and morphology
- Today's code farmer girl summarized her notes about NPM package management and URL module
- BeautifulSoup的使用
- 15th largest value of data flow
- Instructions for mock platform
- TensorFlow张量运算函数集
- Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.
- Self optimization of wireless cell load balancing based on machine learning technology
猜你喜欢

Non progressive phenomena of entropy and morphology

Longest ascending subsequence model acwing 1014. mountaineering

最长上升子序列模型 AcWing 1012. 友好城市

Digital triangle model acwing 275. pass note

推导重叠积分的详细展开式 STO overlap integrals

2022牛客多校 (3)J.Journey

The open source project Taier version 1.1 was officially released, and the list of new functions is fast

背包模型 AcWing 423. 采药

博弈论 AcWing 894. 拆分-Nim游戏

最长上升子序列模型 AcWing 1014. 登山
随机推荐
学习笔记-微信支付
Error: image clipToBoundsAndScale, argument 'input'
9 UAV array
IO流_字符流、IO流小结、IO流案例总结
Chengying, kangaroo cloud one-stop fully automated operation and maintenance steward, is officially open source
6 find the smallest letter larger than the target letter
推导STO双中心动能积分的详细展开式
The article will not keep VIP charges all the time. It will be open for a period of time
背包模型 AcWing 1024. 装箱问题
Asustek unparalleled, this may be the best affordable high brush thin notebook on the screen
IO stream_ Overview and explanation of data input and output flow
Compete for the key battle of stock users and help enterprises build a perfect labeling system - 01 live review
Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?
Play with the cluster configuration center and learn about the Taier console
最长上升子序列模型 AcWing 1010. 拦截导弹
博弈论 AcWing 892. 台阶-Nim游戏
pyquery 的使用
Knapsack model acwing 1024. Packing problem
神经网络学习笔记
洛谷 P3052 [USACO12MAR]Cows in a Skyscraper G