Loading...
Submission
# When Author Problem Language CPU Memory
20893 2024-06-29 20:58:14 AHAMMED_99 Bonus Project C 43 ms 18168 kb Wrong Answer - 1
Test Cases
# CPU Memory Points
1 43 ms 18168 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 MAXN 500001
  5.  
  6. // Structure for storing hierarchy
  7. typedef struct {
  8. int *adj[MAXN];
  9. int size[MAXN];
  10. } Graph;
  11.  
  12. void add_edge(Graph *g, int u, int v) {
  13. g->adj[u][g->size[u]++] = v;
  14. }
  15.  
  16. // Iterative DFS to distribute bonuses
  17. void dfs(Graph *g, int start, long long bonuses[], long long bonusAmount) {
  18. int stack[MAXN];
  19. int top = -1;
  20. stack[++top] = start;
  21.  
  22. while (top != -1) {
  23. int node = stack[top--];
  24. bonuses[node] += bonusAmount;
  25.  
  26. for (int i = 0; i < g->size[node]; ++i) {
  27. stack[++top] = g->adj[node][i];
  28. }
  29. }
  30. }
  31.  
  32. int main() {
  33. int T;
  34. scanf("%d", &T); // Number of test cases
  35.  
  36. while (T--) {
  37. int N, Q;
  38. scanf("%d %d", &N, &Q); // Number of employees and number of projects
  39.  
  40. long long balances[MAXN] = {0}; // Initial account balances
  41. long long bonuses[MAXN] = {0}; // Bonus amounts
  42.  
  43. for (int i = 1; i <= N; ++i) {
  44. scanf("%lld", &balances[i]);
  45. }
  46.  
  47. // Initialize graph
  48. Graph g = {.size = {0}};
  49. for (int i = 1; i <= N; ++i) {
  50. g.adj[i] = (int*) malloc(N * sizeof(int));
  51. }
  52.  
  53. for (int i = 0; i < N - 1; ++i) {
  54. int A, B;
  55. scanf("%d %d", &A, &B);
  56. add_edge(&g, A, B); // A is the boss of B
  57. }
  58.  
  59. for (int i = 0; i < Q; ++i) {
  60. int X, Y;
  61. scanf("%d %d", &X, &Y);
  62. bonuses[X] += 2 * Y; // Team leader gets double the bonus
  63. dfs(&g, X, bonuses, Y); // Subordinates get the bonus
  64. }
  65.  
  66. // Calculate the final balances
  67. for (int i = 1; i <= N; ++i) {
  68. balances[i] += bonuses[i];
  69. printf("%lld ", balances[i]);
  70. }
  71. printf("\n");
  72.  
  73. // Free the allocated memory
  74. for (int i = 1; i <= N; ++i) {
  75. free(g.adj[i]);
  76. }
  77. }
  78.  
  79. return 0;
  80. }
  81.