当前位置:网站首页>(tree DP) acwing 285 A dance without a boss
(tree DP) acwing 285 A dance without a boss
2022-06-13 09:23:00 【Age worry】
285. A dance without a boss
Topic link https://www.acwing.com/problem/content/287/
subject :
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int N=6010;
int w[N];
vector<int > q[N];
bool fa[N];
int f[N][2];
void dfs(int u){
f[u][1]=w[u];
for(int i=0;i<q[u].size();i++){
int j=q[u][i];
dfs(j);
f[u][1]+=f[j][0];
f[u][0]+=max(f[j][0],f[j][1]);
}
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
int x,y;
for(int i=1;i<n;i++){
scanf("%d%d",&x,&y);
q[y].push_back(x);
fa[x]=1;
}
int t=1;
while(fa[t]) t++;
dfs(t);
printf("%d\n",max(f[t][0],f[t][1]));
return 0;
}
边栏推荐
- LeetCode 202. 快乐数
- Neo4j - CQL use
- Neo4j - CQL使用
- @Value不生效及extend/implement其他类无法注入bean的手动注入
- turtle库的使用数字时钟模拟时钟动态显示
- C language: shortcut keys commonly used in VS
- C language: preprocessing in program environment
- [implementation of depth first search]
- Talking about acid of database
- Attack and defense world PWN shell
猜你喜欢
CAS无锁
Storage mode of drawings
C language: dynamic memory management
The turtle library displays the system time
Exporting MySQL data table documents using Navicat
Final principle
C language: preprocessing in program environment
C language: Simulated Implementation of library function strcpy
final 原理
JUC atomic reference and ABA problem
随机推荐
Dpdk timer learning notes
LeetCode 583. Deleting two strings
Download address of QT source code of each version
C language: deep understanding of character functions and string functions (1)
Attack and defense world PWN shell
Neo4j环境搭建
A static variable is associated with a class and can be used as long as the class is in memory (the variable does not exist as long as your application terminates). (heap body, stack reference)
Collection of articles on virtualization and cloud computing
20211108 differential tracker
C/s model and P2P model
20220524 how to install coppeliasim to disk D
20211006 integral, differential and projection belong to linear transformation
LeetCode 6095. Strong password checker II
Final principle
Yolov5 model evaluation
SpEL表达式 简单使用
简单实现数据库链接池
20211018 some special matrices
Alibaba senior experts analyze the standard design of protocol
LeetCode 5289. Fair distribution of cookies (DFS)