当前位置:网站首页>Tree DP acwing 285 A dance without a boss
Tree DP acwing 285 A dance without a boss
2022-07-03 08:51: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
边栏推荐
- Monotonic stack -42 Connect rainwater
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
- Convert video to GIF
- Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
- [concurrent programming] atomic operation CAS
- 树形DP AcWing 285. 没有上司的舞会
- LinkedList set
- Dom4j traverses and updates XML
- 如何应对数仓资源不足导致的核心任务延迟
- Pit & ADB wireless debugging of vivo real machine debugging
猜你喜欢
Markdown learning
Collection interface
[rust notes] 02 ownership
Monotonic stack -503 Next bigger Element II
VIM learning notes from introduction to silk skating
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
Markdown learning
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
Unity editor expansion - controls, layouts
OpenGL learning notes
随机推荐
【Rust 笔记】07-结构体
Dom4j traverses and updates XML
单调栈-503. 下一个更大元素 II
Log4j2 vulnerability recurrence and analysis
【Rust 笔记】13-迭代器(上)
Life cycle of Servlet
How to use Jupiter notebook
高斯消元 AcWing 883. 高斯消元解线性方程组
Parameters of convolutional neural network
【Rust 笔记】12-闭包
Es8 async and await learning notes
分配异常的servlet
[concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
[redis] redis persistent RDB vs AOF (source code)
Unity Editor Extension - drag and drop
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Final review of Database Principles
22-05-26 西安 面试题(01)准备
Six dimensional space (C language)
Try to reprint an article about CSDN reprint