Loading...
Submission
# When Author Problem Language CPU Memory
20848 2024-06-09 11:41:05 AHAMMED_99 Bonus Project C++ 17 12 ms 3408 kb Wrong Answer - 1
Test Cases
# CPU Memory Points
1 12 ms 3408 kb 0 Wrong Answer
2 0 ms 0 kb 0 Skipped
3 0 ms 0 kb 0 Skipped
4 0 ms 0 kb 0 Skipped
5 0 ms 0 kb 0 Skipped
Source Code
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <stack>
  5.  
  6. using namespace std;
  7.  
  8. struct Employee {
  9. int id;
  10. int initialBalance;
  11. int finalBalance;
  12. vector<int> subordinates;
  13. };
  14.  
  15. unordered_map<int, Employee> employees;
  16.  
  17. void distributeBonuses(int teamLeader, int bonus) {
  18. Employee& leader = employees[teamLeader];
  19. leader.finalBalance += bonus * 2;
  20.  
  21. for (int subordinate : leader.subordinates) {
  22. Employee& emp = employees[subordinate];
  23. emp.finalBalance += bonus;
  24. }
  25. }
  26.  
  27. void dfs(int root) {
  28. stack<int> stk;
  29. stk.push(root);
  30.  
  31. while (!stk.empty()) {
  32. int empId = stk.top();
  33. stk.pop();
  34.  
  35. Employee& emp = employees[empId];
  36.  
  37. for (int subordinate : emp.subordinates) {
  38. stk.push(subordinate);
  39. emp.finalBalance += employees[subordinate].finalBalance;
  40. }
  41. }
  42. }
  43.  
  44. int main() {
  45. int t;
  46. cin >> t;
  47.  
  48. while (t--) {
  49. int n, q;
  50. cin >> n >> q;
  51.  
  52. employees.clear();
  53.  
  54. for (int i = 1; i <= n; i++) {
  55. int initialBalance;
  56. cin >> initialBalance;
  57. employees[i] = {i, initialBalance, 0, {}};
  58. }
  59.  
  60. for (int i = 0; i < n - 1; i++) {
  61. int a, b;
  62. cin >> a >> b;
  63. employees[a].subordinates.push_back(b);
  64. }
  65.  
  66. for (int i = 0; i < q; i++) {
  67. int leader, bonus;
  68. cin >> leader >> bonus;
  69. distributeBonuses(leader, bonus);
  70. }
  71.  
  72. dfs(1); // Start DFS from the head of the company
  73.  
  74. for (int i = 1; i <= n; i++) {
  75. cout << employees[i].finalBalance << " ";
  76. }
  77. cout << endl;
  78. }
  79.  
  80. return 0;
  81. }
  82.