当前位置:网站首页>樹形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;
}
原創不易
轉載請標明出處
如果對你有所幫助 別忘啦點贊支持哈
边栏推荐
- UE4 source code reading_ Mobile synchronization
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
- On the difference and connection between find and select in TP5 framework
- [rust notes] 07 structure
- How does unity fixedupdate call at a fixed frame rate
- Alibaba canal actual combat
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- [linear table] basic operation of bidirectional linked list specify node exchange
- Message pack in C deserializes array objects
- [concurrent programming] concurrent tool class of thread
猜你喜欢
[redis] redis persistent RDB vs AOF (source code)
Analysis of Alibaba canal principle
UE4 source code reading_ Mobile synchronization
VIM learning notes from introduction to silk skating
单调栈-42. 接雨水
Message queue for interprocess communication
Gif remove blank frame frame number adjustment
Servlet的生命周期
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
JS ternary operator - learning notes (with cases)
随机推荐
796 · unlock
Life cycle of Servlet
Notes on understanding applets 2022/7/3
TP5 order multi condition sort
[concurrent programming] concurrent tool class of thread
Chocolate installation
DOM 渲染系统(render mount patch)响应式系统
了解小程序的笔记 2022/7/3
[concurrent programming] atomic operation CAS
【Rust 笔记】08-枚举与模式
Intersectionpicker in osgearth
Message pack in C deserializes array objects
Simple demo of solving BP neural network by gradient descent method
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
Unity editor expansion - window, sub window, menu, right-click menu (context menu)
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
Convert video to GIF
树形DP AcWing 285. 没有上司的舞会
如何应对数仓资源不足导致的核心任务延迟
Deep parsing (picture and text) JVM garbage collector (II)