Guest User

Untitled

a guest
Nov 22nd, 2017
74
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.  
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7. int parent,offset,choice,notchoice;
  8. };
  9.  
  10. int main()
  11. {
  12. int ncase;
  13. cin >> ncase;
  14. while(ncase--){
  15. int n;
  16. cin >> n;
  17. node tree[n+1];
  18. for(int i=0;i<=n;i++)
  19. tree[i].offset=0;
  20. cin >>tree[1].choice;
  21. tree[1].parent=-1;
  22. tree[1].notchoice=0;
  23. for(int i=2;i<=n;i++){
  24. cin >> tree[i].parent >> tree[i].choice;
  25. tree[tree[i].parent].offset++;
  26. tree[i].notchoice=0;
  27. }
  28. queue<int> que;
  29. for(int i=1;i<=n;i++){
  30. if(!tree[i].offset)
  31. que.push(i);
  32. }
  33. while(!que.empty()){
  34. int num=que.front();
  35. //cout << tree[num].choice <<" "<<tree[num].notchoice<<endl;
  36. que.pop();
  37. tree[tree[num].parent].offset--;
  38. if(!tree[tree[num].parent].offset)
  39. que.push(tree[num].parent);
  40. tree[tree[num].parent].choice+=tree[num].notchoice;
  41. tree[tree[num].parent].notchoice+=max(tree[num].choice,tree[num].notchoice);
  42. }
  43. cout << max(tree[1].choice,tree[1].notchoice) << endl;
  44. }
  45. return 0;
  46. }
Add Comment
Please, Sign In to add comment