当前位置:网站首页>Arbre DP acwing 285. Un bal sans patron.
Arbre DP acwing 285. Un bal sans patron.
2022-07-03 08:51:00 【T Y F666】
Espace virtuel de code
L'espace interne de la fonction est stocké dans la pile C++La taille par défaut de l'espace de pile est4M Il est donc facile d'ouvrir la pile à l'intérieur de la fonction
Variables globales les variables statiques sont stockées dans le tas
ArbreDP AcWing 285. Un bal sans patron.
Lien vers la question originale
AcWing 285. Un bal sans patron.
Étiquette de l'algorithme
Planification dynamique ArbreDP
Idées

Calcul de l'état

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;
// Requête du noeud racine
while(p[root]){
root++;
}
dfs(root);
printf("%lld", max(f[root][0], f[root][1]));
return 0;
}
L'originalité n'est pas facile
Réimpression Veuillez indiquer la source
Si ça t'aide N'oublie pas de me soutenir.
边栏推荐
- Phpstudy 80 port occupied W10 system
- Mortgage Calculator
- OpenGL learning notes
- Deep parsing (picture and text) JVM garbage collector (II)
- [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
- Apache startup failed phpstudy Apache startup failed
- Sending and receiving of request parameters
- Unity notes 1
- Try to reprint an article about CSDN reprint
- [rust notes] 06 package and module
猜你喜欢

Deeply understand the underlying data structure of MySQL index

too many open files解决方案

Vscode, idea, VIM development tool shortcut keys

Markdown learning
![[RPC] RPC remote procedure call](/img/dc/872204ea47fcff04cdb72e18a2a4ef.jpg)
[RPC] RPC remote procedure call

单调栈-42. 接雨水

单调栈-503. 下一个更大元素 II

Annotations simplify configuration and loading at startup

Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)

JS ternary operator - learning notes (with cases)
随机推荐
【Rust 笔记】10-操作符重载
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
Markdown directory generation
注解简化配置与启动时加载
[rust notes] 08 enumeration and mode
[rust notes] 05 error handling
Unity Editor Extension - Outline
Unity learning notes
Redis cluster series 4
[rust notes] 07 structure
20220630学习打卡
Gradle's method of dynamically modifying APK package name
[public key cryptography] ECC elliptic cryptosystem (implementing ElGamal encryption method)
Pit & ADB wireless debugging of vivo real machine debugging
分配异常的servlet
Visual Studio (VS) shortcut keys
JS ternary operator - learning notes (with cases)
Unity interactive water ripple post-treatment
LinkedList set
How to use Jupiter notebook