当前位置:网站首页>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.
边栏推荐
- [concurrent programming] concurrent tool class of thread
- [concurrent programming] concurrent security
- Concurrent programming (III) detailed explanation of synchronized keyword
- PHP uses foreach to get a value in a two-dimensional associative array (with instances)
- Binary tree sorting (C language, char type)
- Markdown learning
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
- [concurrent programming] explicit lock and AQS
- 使用dlv分析golang进程cpu占用高问题
- SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
猜你喜欢
How to use Jupiter notebook
Binary to decimal, decimal to binary
Allocation exception Servlet
单调栈-84. 柱状图中最大的矩形
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
Unity interactive water ripple post-treatment
Sending and receiving of request parameters
Deeply understand the underlying data structure of MySQL index
[redis] redis persistent RDB vs AOF (source code)
[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
随机推荐
Allocation exception Servlet
Unity notes 1
Drawing maze EasyX library with recursive backtracking method
Alibaba canal actual combat
Analysis of Alibaba canal principle
DOM 渲染系统(render mount patch)响应式系统
[public key cryptography] ECC elliptic cryptosystem (implementing ElGamal encryption method)
Alibaba canaladmin deployment and canal cluster Ha Construction
Deep parsing (picture and text) JVM garbage collector (II)
Deep parsing JVM memory model
Deeply understand the underlying data structure of MySQL index
PHP function date (), y-m-d h:i:s in English case
Convert video to GIF
How to deal with the core task delay caused by insufficient data warehouse resources
Common DOS commands
Monotonic stack -42 Connect rainwater
Query XML documents with XPath
Unity editor expansion - draw lines
分配异常的servlet
VIM learning notes from introduction to silk skating