记录编号 287289 评测结果 AAAAAAAAAA
题目名称 [POJ 2342] 猴腮雷 最终得分 100
用户昵称 GravatarAntiLeaf 是否通过 通过
代码语言 C++ 运行时间 0.000 s
提交时间 2016-08-01 17:38:16 内存使用 0.00 MiB
显示代码纯文本
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<queue>
  5. using namespace std;
  6. const int maxn=10010;
  7. void insert(int,int);
  8. void bfs();
  9. int a[maxn],prt[maxn],b[maxn];
  10. struct edge{
  11. int to;
  12. edge *prev;
  13. edge():to(0),prev(NULL){}
  14. }ee[maxn<<1];
  15. int len=0;
  16. edge *last[maxn]={NULL};
  17. int n,f[maxn][2],x,y;
  18. inline int MAIN(){
  19. #define MINE
  20. #ifdef MINE
  21. freopen("monk.in","r",stdin);
  22. freopen("monk.out","w",stdout);
  23. #endif
  24. scanf("%d",&n);
  25. for(int i=1;i<=n;i++)scanf("%d",&a[i]);
  26. for(int i=1;i<n;i++){
  27. scanf("%d%d",&x,&y);
  28. insert(x,y);
  29. insert(y,x);
  30. }
  31. bfs();
  32. while(n--){
  33. x=b[n+1];
  34. f[x][1]+=a[x];
  35. f[prt[x]][0]+=max(f[x][0],f[x][1]);
  36. f[prt[x]][1]+=f[x][0];
  37. }
  38. printf("%d",max(f[1][0],f[1][1]));
  39. return 0;
  40. }
  41. void insert(int x,int y){
  42. ee[len].to=y;
  43. ee[len].prev=last[x];
  44. last[x]=&ee[len++];
  45. }
  46. void bfs(){
  47. b[0]=0;
  48. queue<int>q;
  49. q.push(1);
  50. int y;
  51. while(!q.empty()){
  52. int &x=q.front();
  53. b[++b[0]]=x;
  54. for(edge *e=last[x];e;e=e->prev){
  55. y=e->to;
  56. if(y==prt[x])continue;
  57. prt[y]=x;
  58. q.push(y);
  59. }
  60. q.pop();
  61. }
  62. }
  63. int hzoier=MAIN();
  64. int main(){;}
  65. /*
  66. 6
  67. 100 100 10 10 100 100
  68. 1 3
  69. 4 5
  70. 2 3
  71. 4 6
  72. 3 4
  73. Answer:
  74. 400
  75. */