不知道哪里错了,求调,谢谢大佬:)

P1352 没有上司的舞会

@[ARTI001](/user/1227031) ```cpp #include<iostream> #include<cstdio> #include<cmath> #include<vector> using namespace std; int n,r[6005],l,k,fa[6005],dp[6005][2]; vector<int>sto[6005]; void dfs(int x) { dp[x][1]=r[x]; for(int i=0;i<sto[x].size();i++) { int next=sto[x][i]; if(next)dfs(next); dp[x][0]+=max(dp[next][1],dp[next][0]); dp[x][1]+=dp[next][0]; } return; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",r+i); for(int i=0;i<n-1;i++) { scanf("%d %d",&l,&k); fa[l]=k; sto[k].push_back(l); } int root=1; while(fa[root]!=0) { root=fa[root]; } dfs(root); cout<<max(dp[root][0],dp[root][1]); return 0; } ```
by _zuoqingyuan @ 2024-01-31 19:42:20


1. 存图用邻接矩阵会空间超限 2. 要判断儿子节点是否为空,不然会一直递归
by _zuoqingyuan @ 2024-01-31 19:43:31


@[zuoqingyuan](/user/731650) 非常感谢!!!
by ARTI001 @ 2024-01-31 19:58:27


|