当前位置:网站首页>树形DP AcWing 285. 没有上司的舞会
树形DP AcWing 285. 没有上司的舞会
2022-07-03 08:41: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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 请求参数的发送和接收
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
- Introduction to hexadecimal coding
- [rust notes] 06 package and module
- Find the intersection of line segments
- Alibaba canal actual combat
- Intersectionpicker in osgearth
- UE4 source code reading_ Mobile synchronization
- Dom4j遍历和更新XML
- Visual Studio (VS) shortcut keys
猜你喜欢

Constraintlayout's constraintset dynamically modifies constraints

Unity editor expansion - draw lines

UE4 source code reading_ Bone model and animation system_ Animation process

Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
![[concurrent programming] explicit lock and AQS](/img/5f/a80751a68726f53d11133810f454a3.jpg)
[concurrent programming] explicit lock and AQS

单调栈-84. 柱状图中最大的矩形

Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template

Message queue for interprocess communication

Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)

22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
随机推荐
Jupyter remote server configuration and server startup
[concurrent programming] collaboration between threads
单调栈-42. 接雨水
[concurrent programming] working mechanism and type of thread pool
Query XML documents with XPath
【Rust 笔记】13-迭代器(上)
[redis] redis persistent RDB vs AOF (source code)
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
Redux - learning notes
[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
Advanced OSG collision detection
DOM 渲染系统(render mount patch)响应式系统
Drawing maze EasyX library with recursive backtracking method
[rust notes] 08 enumeration and mode
Binary to decimal, decimal to binary
Unity multi open script
Alibaba canal actual combat
TP5 multi condition sorting
[rust notes] 13 iterator (Part 1)
Phpstudy 80 port occupied W10 system