This commit is contained in:
Zengtudor 2024-09-07 19:05:21 +08:00
parent 3ee6d3ba3d
commit c69cdd105f
3 changed files with 84 additions and 11 deletions

53
20240907/P1352/P1352.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <algorithm>
#include <cassert>
#include <iostream>
#include <istream>
#include <vector>
const int MAX_N=6e3+5;
struct Node{
std::vector<int> sons;
int r;
}nodes[MAX_N];
bool had_father[MAX_N];
int dp[MAX_N][2];
void dfs(const int n){
dp[n][1]=nodes[n].r;
for(auto &i:nodes[n].sons){
dfs(i);
dp[n][0]+=std::max(dp[i][1],dp[i][0]);
dp[n][1]+=dp[i][0];
}
}
int main(){
std::iostream::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0);
int n;
std::cin>>n;
for(int i=1;i<=n;i++){
int _r;
std::cin>>_r;
nodes[i].r=_r;
}
for(int i=1;i<n;i++){
int s,f;
std::cin>>s>>f;
nodes[f].sons.push_back(s);
had_father[s]=true;
}
int master{-1};
for(int i=1;i<=n;i++){
if(had_father[i]==false){
master=i;
}
}
assert(master!=-1);
dfs(master);
std::cout<<std::max(dp[master][0],dp[master][1])<<"\n";
}

View File

@ -1,21 +1,38 @@
//using C++17
#include <cmath> #include <cmath>
#include<iostream> #include<iostream>
#include <vector>
std::ostream& operator<<(std::ostream &os,const std::vector<int> &v){
os<<"vector { ";
for(int i=0;i<v.size()-1;i++){
os<<v[i]<<" ,";
}
os<<v[v.size()-1]<<" }";
return os;
}
template<typename ...Args>
void print(const Args& ...args){
((std::cout<<args),...);
}
template<typename ...Args> template<typename ...Args>
void println(const Args& ...args){ void println(const Args& ...args){
((std::cout<<args<<" "),...); (print(args," "),...);
std::cout<<"\n"; print("\n");
} }
#define NAME_VALUE(v)#v,":",(v)
int main(){ int main(){
println("Hello,World!","-by","Zengtudor"); println("Hello,World!","-by","Zengtudor");
println("End"); println("End");
println(std::pow(2,10)); println(NAME_VALUE(std::pow(2,10)));
println(NAME_VALUE((int)0x7FFFFFFF));
std::vector<int> v(10);
v[0]=1;
for(int i=1;i<v.size();i++){
v[i]=(v[i-1]*3+2)%49;
}
println(v);
} }
/*
Hello,World! -by Zengtudor
End
*/

View File

@ -46,3 +46,6 @@ target("test")
target("bitset") target("bitset")
add_files("20240906/bitset.cpp") add_files("20240906/bitset.cpp")
target("P1352")
add_files("20240907/P1352/P1352.cpp")