Loading...
Submission
# When Author Problem Language CPU Memory
20892 2024-06-29 20:56:38 AHAMMED_99 Bonus Project C++ 14 13 ms 3384 kb Wrong Answer - 1
Test Cases
# CPU Memory Points
1 13 ms 3384 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 <algorithm>
  4. using namespace std;
  5.  
  6. // Function to perform DFS and calculate the bonus for each employee
  7. void dfs(int node, const vector<vector<int>>& adj, vector<long long>& bonuses, long long bonusAmount) {
  8. bonuses[node] += bonusAmount; // Add the bonus to the current node
  9. for (int child : adj[node]) {
  10. dfs(child, adj, bonuses, bonusAmount); // Recursively add the bonus to subordinates
  11. }
  12. }
  13.  
  14. int main() {
  15. int T;
  16. cin >> T; // Number of test cases
  17. while (T--) {
  18. int N, Q;
  19. cin >> N >> Q; // Number of employees and number of projects
  20.  
  21. vector<long long> balances(N + 1); // Initial account balances
  22. for (int i = 1; i <= N; ++i) {
  23. cin >> balances[i];
  24. }
  25.  
  26. vector<vector<int>> adj(N + 1); // Adjacency list for the company's hierarchy
  27. for (int i = 0; i < N - 1; ++i) {
  28. int A, B;
  29. cin >> A >> B;
  30. adj[A].push_back(B); // A is the boss of B
  31. }
  32.  
  33. vector<long long> bonuses(N + 1, 0); // To store the bonus amounts for each employee
  34. for (int i = 0; i < Q; ++i) {
  35. int X, Y;
  36. cin >> X >> Y;
  37. bonuses[X] += 2 * Y; // Team leader gets double the bonus
  38. for (int sub : adj[X]) {
  39. dfs(sub, adj, bonuses, Y); // Subordinates get the bonus
  40. }
  41. }
  42.  
  43. // Calculate the final balances
  44. for (int i = 1; i <= N; ++i) {
  45. balances[i] += bonuses[i];
  46. cout << balances[i] << " ";
  47. }
  48. cout << endl;
  49. }
  50.  
  51. return 0;
  52. }
  53.