当前位置:网站首页>樹形DP AcWing 285. 沒有上司的舞會
樹形DP AcWing 285. 沒有上司的舞會
2022-07-03 08:51: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;
}
原創不易
轉載請標明出處
如果對你有所幫助 別忘啦點贊支持哈
边栏推荐
- How to delete CSDN after sending a wrong blog? How to operate quickly
- Find the intersection of line segments
- [set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
- Pit & ADB wireless debugging of vivo real machine debugging
- [rust notes] 08 enumeration and mode
- Graphics_ Learnopongl learning notes
- [concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
- TP5 multi condition sorting
- Unity editor expansion - controls, layouts
- VIM learning notes from introduction to silk skating
猜你喜欢
![[RPC] RPC remote procedure call](/img/dc/872204ea47fcff04cdb72e18a2a4ef.jpg)
[RPC] RPC remote procedure call

Markdown learning

JS non Boolean operation - learning notes
![[concurrent programming] working mechanism and type of thread pool](/img/51/d21428a7c95c0a5177e8198742e78c.jpg)
[concurrent programming] working mechanism and type of thread pool

Mortgage Calculator

Es8 async and await learning notes

了解小程序的笔记 2022/7/3

【Rust笔记】02-所有权

Constraintlayout's constraintset dynamically modifies constraints

Message queue for interprocess communication
随机推荐
[rust notes] 09- special types and generics
【Rust 笔记】13-迭代器(上)
Dealing with duplicate data in Excel with xlwings
How to deal with the core task delay caused by insufficient data warehouse resources
单调栈-84. 柱状图中最大的矩形
Markdown learning
Monotonic stack -42 Connect rainwater
Notes on understanding applets 2022/7/3
Unity Editor Extension - Outline
Alibaba canal actual combat
Parameters of convolutional neural network
Dom4j traverses and updates XML
Deeply understand the underlying data structure of MySQL index
20220630学习打卡
Unity Editor Extension - drag and drop
Allocation exception Servlet
[rust note] 10 operator overloading
Sending and receiving of request parameters
UE4 source code reading_ Bone model and animation system_ Animation node
Cesium for unreal quick start - simple scenario configuration