当前位置:网站首页>Tree DP acwing 285. dance without boss
Tree DP acwing 285. dance without boss
2022-07-27 11:18:00 【T_ Y_ F666】
Code virtual space
The internal space of the function is stored in the stack C++ The default stack space size is 4M Therefore, it is easy to burst the stack inside the function
Global variables static variables are stored in the heap
Tree form DP AcWing 285. A dance without a boss
Original link
AcWing 285. A dance without a boss
Algorithm tags
Dynamic programming Tree form DP
Ideas

State calculation

Code
#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;
// Query root node
while(p[root]){
root++;
}
dfs(root);
printf("%lld", max(f[root][0], f[root][1]));
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- 15th largest value of data flow
- Miscellaneous records of Finance
- 如何组装一个注册中心
- Study notes Minio
- Longest ascending subsequence model acwing 1014. mountaineering
- 解决 ImportError: cannot import name 'abs' 导入tensorflow报错
- Internal and external troubles of digital collection NFT "boring ape" bayc
- 6 find the smallest letter larger than the target letter
- Yum source installation
- Remember not to copy your group work, students. Fortunately, you only passed two questions. Don't have an accident
猜你喜欢

A deep analysis of the soul of C language -- pointer

XXX packages are looking for funding run 'NPM fund' for details solutions

How to build a real-time development platform to deeply release the value of enterprise real-time data?

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

区间问题 AcWing 906. 区间分组

高斯消元 AcWing 884. 高斯消元解异或线性方程组

计算重叠积分的第二种方法

深析C语言的灵魂 -- 指针

Budweiser, a well-known beer, plans to launch NFT in an attempt to unveil the "long planned" uplink?

An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft
随机推荐
Students, don't copy all my code, remember to change it, or we both want G
【着色器实现Shake随机摇动效果_Shader效果第十篇】
Miscellaneous records of Finance
KEPServer配置
12 is at least twice the maximum number of other numbers
博弈论 AcWing 894. 拆分-Nim游戏
pyquery 的使用
计算重叠积分的第二种方法
中国剩余定理 AcWing 204. 表达整数的奇怪方式
Wechat push - template message parameter configuration
Real time development platform construction practice, in-depth release of real-time data value - 04 live broadcast review
Knapsack model acwing 1024. Packing problem
TensorFlow张量运算函数集
Ansible
数字三角形模型 AcWing 275. 传纸条
Kepserver configuration
Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?
深析C语言的灵魂 -- 指针
4 search insertion location
ethereum rpc