Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- using ld = long double;
- ll solve(int v, vector<ll>& arr, vector<vector<int>>& tree, bool f) {
- ll ans = arr[v];
- for (int to: tree[v]) {
- ans += solve(to, arr, tree, f) * (f ? 2 : 1);
- }
- return ans;
- }
- int main(int argc, char **argv) {
- ios::sync_with_stdio(false);
- srand(time(0));
- #ifdef HOME
- freopen("/home/acarus/Desktop/io/input.txt", "r", stdin);
- // freopen("/home/acarus/Desktop/io/output.txt", "w", stdout);
- #endif
- int n;
- cin >> n;
- vector<ll> v(n + 1);
- for (int i = 1; i <= n; ++i) cin >> v[i];
- vector<vector<int>> tree(n + 1, vector<int>());
- for (int i = 1; i <= n; ++i) {
- int k, to;
- cin >> k;
- for (int j = 0; j < k; ++j) {
- cin >> to;
- tree[i].push_back(to);
- }
- }
- cout << solve(1, v, tree, false) << " " << solve(1, v, tree, true) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement