Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. using ld = long double;
  7.  
  8. ll solve(int v, vector<ll>& arr, vector<vector<int>>& tree, bool f) {
  9. ll ans = arr[v];
  10. for (int to: tree[v]) {
  11. ans += solve(to, arr, tree, f) * (f ? 2 : 1);
  12. }
  13. return ans;
  14. }
  15.  
  16. int main(int argc, char **argv) {
  17. ios::sync_with_stdio(false);
  18. srand(time(0));
  19.  
  20. #ifdef HOME
  21. freopen("/home/acarus/Desktop/io/input.txt", "r", stdin);
  22. // freopen("/home/acarus/Desktop/io/output.txt", "w", stdout);
  23. #endif
  24.  
  25. int n;
  26. cin >> n;
  27.  
  28. vector<ll> v(n + 1);
  29. for (int i = 1; i <= n; ++i) cin >> v[i];
  30.  
  31. vector<vector<int>> tree(n + 1, vector<int>());
  32. for (int i = 1; i <= n; ++i) {
  33. int k, to;
  34. cin >> k;
  35.  
  36. for (int j = 0; j < k; ++j) {
  37. cin >> to;
  38. tree[i].push_back(to);
  39. }
  40. }
  41. cout << solve(1, v, tree, false) << " " << solve(1, v, tree, true) << endl;
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement