Skip to content

Instantly share code, notes, and snippets.

@Acarus
Created April 30, 2017 20:39
Show Gist options
  • Save Acarus/5d7854d4ea0cca79b44568fb332daf04 to your computer and use it in GitHub Desktop.
Save Acarus/5d7854d4ea0cca79b44568fb332daf04 to your computer and use it in GitHub Desktop.
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment