当前位置:网站首页>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.
边栏推荐
- Find the intersection of line segments
- XPath实现XML文档的查询
- Deep parsing JVM memory model
- 【Rust笔记】05-错误处理
- UE4 source code reading_ Bone model and animation system_ Animation compression
- Unity editor expansion - scrolling list
- Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
- [rust notes] 07 structure
- [public key cryptography] ECC elliptic cryptosystem (implementing ElGamal encryption method)
- 高斯消元 AcWing 883. 高斯消元解线性方程组
猜你喜欢

Final review of Database Principles

Es8 async and await learning notes

Deep parsing JVM memory model

First Servlet

Binary tree sorting (C language, int type)

XPath实现XML文档的查询

Vscode, idea, VIM development tool shortcut keys

Facial expression recognition based on pytorch convolution -- graduation project

Try to reprint an article about CSDN reprint

Gif remove blank frame frame number adjustment
随机推荐
Dom4j traverses and updates XML
Unity notes 1
MySQL index types B-tree and hash
Dealing with duplicate data in Excel with xlwings
Cesium for unreal quick start - simple scenario configuration
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
Collection interface
Markdown learning
Deep parsing JVM memory model
Binary to decimal, decimal to binary
Graphics_ Learnopongl learning notes
Monotonic stack -503 Next bigger Element II
UE4 source code reading_ Bone model and animation system_ Animation node
LinkedList set
Gif remove blank frame frame number adjustment
记忆化搜索 AcWing 901. 滑雪
php public private protected
UE4 source code reading_ Bone model and animation system_ Animation compression
Six dimensional space (C language)
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation