willy108

Why you use templates

Jul 25th, 2024
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1.  
  2. //misaka and rin will carry me to cm
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <utility>
  7. #include <cassert>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <array>
  11. #include <tuple>
  12.  
  13. #define ll long long
  14. #define lb long double
  15. #define sz(vec) ((int)(vec.size()))
  16. #define all(x) x.begin(), x.end()
  17. #define scam(v) sum[v] - dp[v] + arr[v]
  18. const lb eps = 1e-9;
  19. const ll mod = 1e9 + 7, ll_max = 1e18;
  20. //const ll mod = (1 << (23)) * 119 +1;
  21. const int MX = 1e5 +10, int_max = 0x3f3f3f3f;
  22.  
  23. using namespace std;
  24.  
  25. int n;
  26. ll dp[MX], tot[MX], arr[MX], tim[MX], sum[MX];
  27. //arr, time are input
  28. //tot = sum_{v in child of u} arr[v]
  29. //sum = sum_{v in child of u} dp[v]
  30. //dp[u] best cost
  31. vector<int> adj[MX];
  32.  
  33. //ll scam(int v){
  34. //  return sum[v] - dp[v] + arr[v];
  35. //}
  36.  
  37. void dfs(int u, int p){
  38.     int cand = 0, cand2 = 0; ll best = 0, best2 = 0; //candidate for time[v] = 3
  39.     ll lag = 0, lag3 = 0;
  40.     for(int v : adj[u]){
  41.         if(v == p) continue;
  42.         dfs(v, u);
  43.         sum[u] += dp[v];
  44.         lag = max(lag, arr[v]);
  45.                 if(scam(v) > best){
  46.             cand2 = cand, best2 = best;
  47.             cand = v, best = scam(v);
  48.         }else if(scam(v) > best2){
  49.             cand2 = v, best2 = scam(v);
  50.         }
  51.     }
  52.     for(int v : adj[u]){
  53.         if(tim[v] == 3 && v != cand && v != p) lag3 = max(arr[v], lag3);
  54.     }
  55.     dp[u] = max(lag, lag3 + best);
  56.     if(tim[cand] == 3) dp[u] = max(dp[u], best2 + arr[cand]);
  57.     dp[u] += sum[u];
  58. }
  59.  
  60.  
  61. void solve(){
  62.     cin >> n;
  63.     for(int i = 0; i<=n; i++){
  64.         tot[i] = dp[i] = sum[i] = 0;
  65.         adj[i].clear();
  66.     }
  67.     for(int i = 1; i<=n; i++) cin >> arr[i];
  68.     for(int i = 1; i<=n; i++) cin >> tim[i];
  69.     for(int i = 1; i<n; i++){
  70.         int a, b; cin >> a >> b;
  71.         adj[a].push_back(b);
  72.         b[adj].push_back(a);
  73.     }
  74.     dfs(1, 0);
  75.     cout << dp[1] +arr[1] << "\n";
  76. }
  77.    
  78. int main(){
  79.     int T; cin >> T;
  80.     while(T--){
  81.         solve();
  82.     }
  83.     return 0;
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment