Beingamanforever

1822F - Gardening Friends, Subtree DP

Jan 14th, 2025
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define all(x) (x).begin(), (x).end()
  5. typedef vector<int> vi;
  6. typedef vector<vi> vvi;
  7. typedef vector<pair<int, int>> vpi;
  8. typedef pair<int, int> pi;
  9. #define f first
  10. #define s second
  11. #define pb push_back
  12. #define endl "\n"
  13. #define yes cout << "YES" << endl
  14. #define no cout << "NO" << endl
  15. const int mod1 = 1e9 + 7, mod2 = 998244353, INF = 2e18, N = 2e5 + 5, L = 19;
  16. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  17. // -----------------------------------------------------------------------------
  18.  
  19. void solve()
  20. {
  21.     int n, k, c;
  22.     cin >> n >> k >> c;
  23.     vi adj[n + 1];
  24.     for (int i = 1; i <= (n - 1); i++)
  25.     {
  26.         int u, v;
  27.         cin >> u >> v;
  28.         adj[u].pb(v);
  29.         adj[v].pb(u);
  30.     }
  31.     vi dep(n + 1, 0), ht(n + 1, 0);
  32.     // depth from root, ht of subtree rooted at u
  33.     function<void(int, int, int)> dfs1 = [&](int u, int d, int p)
  34.     {
  35.         dep[u] = d;
  36.         ht[u] = -1;
  37.         for (auto v : adj[u])
  38.         {
  39.             if (v != p)
  40.             {
  41.                 dfs1(v, d + 1, u);
  42.                 ht[u] = max(ht[u], ht[v]);
  43.             }
  44.         }
  45.         ht[u]++;
  46.     };
  47.     dfs1(1, 0, 0);
  48.     int ans = 0;
  49.     function<void(int, int, int)> dfs2 = [&](int u, int val, int p)
  50.     {
  51.         // val is max distance from node u, which is not in it's subtree
  52.         vi x;
  53.         x.pb(val);
  54.         for (auto v : adj[u])
  55.         {
  56.             if (v != p)
  57.             {
  58.                 x.pb(ht[v] + 1);
  59.             }
  60.         }
  61.         sort(all(x));
  62.         ans = max(ans, x.back() * k - dep[u] * c);
  63.         for (auto v : adj[u])
  64.         {
  65.             if (v != p)
  66.             {
  67.                 if (ht[v] + 1 == x.back())
  68.                 {
  69.                     // means it came from u's subtree
  70.                     dfs2(v, x[x.size() - 2] + 1, u);
  71.                 }
  72.                 else
  73.                 {
  74.                     // means it came from u's subtree
  75.                     dfs2(v, x[x.size() - 1] + 1, u);
  76.                 }
  77.             }
  78.         }
  79.     };
  80.     dfs2(1, 0, 0);
  81.     cout << ans << endl;
  82.     return;
  83. }
  84.  
  85. signed main()
  86. {
  87.     // __START__;
  88.     ios_base::sync_with_stdio(false);
  89.     cin.tie(NULL);
  90.     cout.tie(NULL);
  91.     int t = 1;
  92.     cin >> t;
  93.     while (t--)
  94.     {
  95.         solve();
  96.     }
  97.     // __END__;
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment