当前位置:网站首页>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 
边栏推荐
- On the difference and connection between find and select in TP5 framework
- Collection interface
- Gif remove blank frame frame number adjustment
- Divide candy (circular queue)
- 【Rust 笔记】11-实用特型
- 【Rust 笔记】12-闭包
- [concurrent programming] working mechanism and type of thread pool
- I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
- 【Rust 笔记】10-操作符重载
- Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
猜你喜欢

Character pyramid

Monotonic stack -84 The largest rectangle in the histogram
![[concurrent programming] explicit lock and AQS](/img/5f/a80751a68726f53d11133810f454a3.jpg)
[concurrent programming] explicit lock and AQS

MySQL 8

Graphics_ Games101/202 learning notes

VIM learning notes from introduction to silk skating

Annotations simplify configuration and loading at startup

too many open files解决方案

基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程

Alibaba canal actual combat
随机推荐
Drawing maze EasyX library with recursive backtracking method
Monotonic stack -84 The largest rectangle in the histogram
Unity editor expansion - controls, layouts
Deep parsing (picture and text) JVM garbage collector (II)
Alibaba canal actual combat
Unity editor expansion - window, sub window, menu, right-click menu (context menu)
How to delete CSDN after sending a wrong blog? How to operate quickly
【Rust笔记】06-包和模块
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
Graphics_ Games101/202 learning notes
[rust notes] 02 ownership
Final review of Database Principles
UE4 source code reading_ Bone model and animation system_ Animation process
Animation_ IK overview
Convert video to GIF
Query XML documents with XPath
【Rust 笔记】11-实用特型
UE4 source code reading_ Mobile synchronization
[rust notes] 07 structure
file_ put_ contents