Loading...
Submission
# When Author Problem Language CPU Memory
19962 2024-05-16 21:24:19 AHAMMED_99 Bonus Project C 13 ms 17384 kb Wrong Answer - 1
Test Cases
# CPU Memory Points
1 13 ms 17384 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 <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX_EMPLOYEES 500005
  5.  
  6. // Structure to represent each employee node
  7. struct Employee {
  8. int id; // Employee ID
  9. int balance; // Initial account balance
  10. int projects; // Number of projects assigned
  11. int bonus; // Bonus amount
  12. struct Employee *subordinates; // Array of subordinates
  13. int numSubordinates; // Number of subordinates
  14. };
  15.  
  16. // Function to perform depth-first traversal
  17. void dfs(struct Employee *employees, int id, int bonus) {
  18. // Add bonus to current employee's balance
  19. employees[id].balance += bonus;
  20.  
  21. // Traverse subordinates recursively
  22. for (int i = 0; i < employees[id].numSubordinates; i++) {
  23. int subordinateID = employees[id].subordinates[i].id;
  24. dfs(employees, subordinateID, bonus);
  25. }
  26. }
  27.  
  28. int main() {
  29. int T;
  30. scanf("%d", &T);
  31.  
  32. while (T--) {
  33. int N, Q;
  34. scanf("%d %d", &N, &Q);
  35.  
  36. struct Employee employees[MAX_EMPLOYEES];
  37. for (int i = 1; i <= N; i++) {
  38. scanf("%d", &employees[i].balance);
  39. employees[i].id = i;
  40. employees[i].projects = 0;
  41. employees[i].subordinates = NULL;
  42. employees[i].numSubordinates = 0;
  43. }
  44.  
  45. // Construct the hierarchy
  46. for (int i = 1; i < N; i++) {
  47. int A, B;
  48. scanf("%d %d", &A, &B);
  49.  
  50. employees[A].numSubordinates++;
  51. employees[A].subordinates = realloc(employees[A].subordinates, employees[A].numSubordinates * sizeof(struct Employee));
  52. employees[A].subordinates[employees[A].numSubordinates - 1] = employees[B];
  53. }
  54.  
  55. // Process each project
  56. for (int i = 0; i < Q; i++) {
  57. int X, Y;
  58. scanf("%d %d", &X, &Y);
  59.  
  60. // Assign bonus to team leader
  61. employees[X].balance += 2 * Y;
  62.  
  63. // Assign bonus to subordinates
  64. dfs(employees, X, Y);
  65. }
  66.  
  67. // Print the final account balances
  68. for (int i = 1; i <= N; i++) {
  69. printf("%d ", employees[i].balance);
  70. }
  71. printf("\n");
  72. }
  73.  
  74. return 0;
  75. }
  76.