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 |
Source Code
#include <stdio.h> #include <stdlib.h> #define MAXN 500001 // Structure for storing hierarchy typedef struct { int *adj[MAXN]; int size[MAXN]; } Graph; void add_edge(Graph *g, int u, int v) { g->adj[u][g->size[u]++] = v; } // Iterative DFS to distribute bonuses void dfs(Graph *g, int start, long long bonuses[], long long bonusAmount) { int stack[MAXN]; int top = -1; stack[++top] = start; while (top != -1) { int node = stack[top--]; bonuses[node] += bonusAmount; for (int i = 0; i < g->size[node]; ++i) { stack[++top] = g->adj[node][i]; } } } int main() { int T; while (T--) { int N, Q; long long balances[MAXN] = {0}; // Initial account balances long long bonuses[MAXN] = {0}; // Bonus amounts for (int i = 1; i <= N; ++i) { } // Initialize graph Graph g = {.size = {0}}; for (int i = 1; i <= N; ++i) { } for (int i = 0; i < N - 1; ++i) { int A, B; add_edge(&g, A, B); // A is the boss of B } for (int i = 0; i < Q; ++i) { int X, Y; bonuses[X] += 2 * Y; // Team leader gets double the bonus dfs(&g, X, bonuses, Y); // Subordinates get the bonus } // Calculate the final balances for (int i = 1; i <= N; ++i) { balances[i] += bonuses[i]; } // Free the allocated memory for (int i = 1; i <= N; ++i) { } } return 0; }