Guest User

Untitled

a guest
Nov 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef struct
  5. {
  6. int parent,value,choice,no,outdegree;
  7. }vertex;
  8.  
  9. void sol()
  10. {
  11. int N=0,tmp=0;
  12. scanf("%d",&N);
  13. vertex v[N+1];
  14. for(size_t i=0; i<=N; i++)
  15. v[i].outdegree = v[i].no = 0;
  16.  
  17. scanf("%d",&v[1].value);
  18. v[1].choice = v[1].value; //the initial value of choice
  19. for(size_t i=2; i<=N; i++)
  20. {
  21. scanf("%d%d",&v[i].parent,&v[i].value);
  22. v[i].choice = v[i].value;
  23. v[v[i].parent].outdegree++;
  24. }
  25.  
  26. queue<int> tree;
  27.  
  28. for(size_t i=2; i<=N; i++)
  29. {
  30. if(v[i].outdegree == 0)
  31. tree.push(i);
  32. }
  33.  
  34. while(!tree.empty())
  35. {
  36. tmp = tree.front();
  37. tree.pop();
  38.  
  39. v[v[tmp].parent].choice += v[tmp].no;
  40. v[v[tmp].parent].no += max(v[tmp].choice,v[tmp].no);
  41. v[v[tmp].parent].outdegree--;
  42.  
  43. if(v[v[tmp].parent].outdegree == 0 && v[tmp].parent != 1)
  44. tree.push(v[tmp].parent);
  45. }
  46. printf("%d\n",max(v[1].choice,v[1].no));
  47. }
  48. int main()
  49. {
  50. int T;
  51. scanf("%d",&T);
  52. while(T--)
  53. sol();
  54. return 0;
  55. }
Add Comment
Please, Sign In to add comment