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 |
Source Code
#include <iostream> #include <vector> #include <algorithm> using namespace std; // Function to perform DFS and calculate the bonus for each employee void dfs(int node, const vector<vector<int>>& adj, vector<long long>& bonuses, long long bonusAmount) { bonuses[node] += bonusAmount; // Add the bonus to the current node for (int child : adj[node]) { dfs(child, adj, bonuses, bonusAmount); // Recursively add the bonus to subordinates } } int main() { int T; cin >> T; // Number of test cases while (T--) { int N, Q; cin >> N >> Q; // Number of employees and number of projects vector<long long> balances(N + 1); // Initial account balances for (int i = 1; i <= N; ++i) { cin >> balances[i]; } vector<vector<int>> adj(N + 1); // Adjacency list for the company's hierarchy for (int i = 0; i < N - 1; ++i) { int A, B; cin >> A >> B; adj[A].push_back(B); // A is the boss of B } vector<long long> bonuses(N + 1, 0); // To store the bonus amounts for each employee for (int i = 0; i < Q; ++i) { int X, Y; cin >> X >> Y; bonuses[X] += 2 * Y; // Team leader gets double the bonus for (int sub : adj[X]) { dfs(sub, adj, bonuses, Y); // Subordinates get the bonus } } // Calculate the final balances for (int i = 1; i <= N; ++i) { balances[i] += bonuses[i]; cout << balances[i] << " "; } cout << endl; } return 0; }