记录编号 543838 评测结果 AAAAAAAAAA
题目名称 没有上司的舞会 最终得分 100
用户昵称 Gravatar牛掰格拉斯 是否通过 通过
代码语言 C++ 运行时间 0.027 s
提交时间 2019-10-10 21:01:25 内存使用 13.89 MiB
显示代码纯文本
  1. //2019.10.10
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. vector<int> tree[6001];
  5. int n,root=1;
  6. int dp[6001][2];//dp[i][1]表示取这个点,dp[i][0]表示不取
  7. int father[6001],happy[6001];
  8. void findroot()
  9. {
  10. while(father[root]!=-1) root=father[root];
  11. }
  12. void dfs(int u)
  13. {
  14. dp[u][0]=0;
  15. dp[u][1]=happy[u];
  16. for(int i=0;i<tree[u].size();i++)
  17. {
  18. int son=tree[u][i];
  19. dfs(son);
  20. dp[u][1]+=dp[son][0];
  21. dp[u][0]+=max(dp[son][0],dp[son][1]);
  22. }
  23. }
  24. int main()
  25. {
  26. freopen("partyy.in","r",stdin);
  27. freopen("partyy.out","w",stdout);
  28. cin>>n;
  29. for(int i=1;i<=n;i++)
  30. {
  31. cin>>happy[i];
  32. father[i]=-1;
  33. }
  34. while(true)
  35. {
  36. int x,y;
  37. cin>>x>>y;
  38. if(x==0&&y==0) break;
  39. tree[y].push_back(x);
  40. father[x]=y;
  41. }
  42. findroot();
  43. dfs(root);
  44. cout<<max(dp[root][0],dp[root][1]);
  45. return 0;
  46. }