Advertisement
i_love_rao_khushboo

D. Eternal Victory

Sep 9th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.03 KB | None | 0 0
  1. // Problem: D. Eternal Victory
  2. // Contest: Codeforces - Codeforces Beta Round #57 (Div. 2)
  3. // URL: https://codeforces.com/contest/61/problem/D
  4. // Memory Limit: 256 MB
  5. // Time Limit: 2000 ms
  6. // Parsed on: 10-09-2021 09:37:05 IST (UTC+05:30)
  7. // Author: Kapil Choudhary
  8. // ********************************************************************
  9. // कर्मण्येवाधिकारस्ते मा फलेषु कदाचन |
  10. // मा कर्मफलहेतुर्भूर्मा ते सङ्गोऽस्त्वकर्मणि || १.४७ ||
  11. // ********************************************************************
  12.  
  13. #include<bits/stdc++.h>
  14. using namespace std;
  15.  
  16. #define ll long long
  17. #define ld long double
  18. #define ull unsigned long long
  19. #define pb push_back
  20. #define ppb pop_back
  21. #define mp make_pair
  22. #define F first
  23. #define S second
  24. #define PI 3.1415926535897932384626
  25. #define sz(x) ((int)(x).size())
  26. #define vset(v, n, val) v.clear(); v.resize(n, val)
  27.  
  28. typedef pair<int, int> pii;
  29. typedef pair<ll, ll> pll;
  30. typedef vector<int> vi;
  31. typedef vector<ll> vll;
  32. typedef vector<ull> vull;
  33. typedef vector<bool> vb;
  34. typedef vector<char> vc;
  35. typedef vector<string> vs;
  36. typedef vector<pii> vpii;
  37. typedef vector<pll> vpll;
  38. typedef vector<vi> vvi;
  39. typedef vector<vll> vvll;
  40. typedef vector<vull> vvull;
  41. typedef vector<vb> vvb;
  42. typedef vector<vc> vvc;
  43. typedef vector<vs> vvs;
  44.  
  45. /************************************************** DEBUGGER *******************************************************************************************************/
  46.  
  47. #ifndef ONLINE_JUDGE
  48. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  49. #else
  50. #define debug(x)
  51. #endif
  52.  
  53. void _print(ll t) { cerr << t; }
  54. void _print(int t) { cerr << t; }
  55. void _print(string t) { cerr << t; }
  56. void _print(char t) { cerr << t; }
  57. void _print(ld t) { cerr << t; }
  58. void _print(double t) { cerr << t; }
  59. void _print(ull t) { cerr << t; }
  60.  
  61. template <class T, class V> void _print(pair <T, V> p);
  62. template <class T> void _print(vector <T> v);
  63. template <class T> void _print(vector <vector<T>> v);
  64. template <class T> void _print(set <T> v);
  65. template <class T, class V> void _print(map <T, V> v);
  66. template <class T> void _print(multiset <T> v);
  67. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  68. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  69. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  70. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  71. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  72. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  73.  
  74. /*******************************************************************************************************************************************************************/
  75.  
  76. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  77. int rng(int lim) {
  78.     uniform_int_distribution<int> uid(0,lim-1);
  79.     return uid(rang);
  80. }
  81.  
  82. const int INF = 0x3f3f3f3f;
  83. const int mod = 1e9+7;
  84.  
  85. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  86.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  87.                          
  88. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  89. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  90.  
  91. /******************************************************************************************************************************/
  92.  
  93. vector<vpll> g;
  94.  
  95. // dp[i][0] = minimum possible traverse for subtree rooted at i if the path starts
  96. //            from node i & ends at node located somewhere in the subtree rooted at i
  97.  
  98. // dp[i][1] = minimum possible traverse for subtree rooted at i if the path starts as
  99. //            well as ends at node i
  100. ll dp[100001][2];
  101.  
  102. int n;
  103.  
  104. bool is_leaf(int cur) {
  105.     return (cur != 1) and (sz(g[cur]) == 1);
  106. }
  107.  
  108. void dfs(int cur, int par) {
  109.     // check for base case
  110.     if(is_leaf(cur)) {
  111.         dp[cur][0] = dp[cur][1] = 0LL;
  112.         return;
  113.     }
  114.        
  115.     // calculate the values for all the children of cur
  116.     for(auto x: g[cur]) {
  117.         if(x.F == par) continue;
  118.         dfs(x.F, cur);
  119.     }
  120.    
  121.     // calculate the values for cur
  122.     dp[cur][1] = 0LL;
  123.    
  124.     for(auto x: g[cur]) {
  125.         if(x.F == par) continue;
  126.         dp[cur][1] += (x.S + dp[x.F][1] + x.S);
  127.     }
  128.    
  129.     dp[cur][0] = LLONG_MAX;
  130.     ll sum = dp[cur][1];
  131.        
  132.     for(auto x: g[cur]) {
  133.         if(x.F == par) continue;
  134.        
  135.         // value of traverse if the path ends in subtree of child of cur
  136.         ll val = sum - x.S - dp[x.F][1] + dp[x.F][0];
  137.         dp[cur][0] = min(dp[cur][0], val);
  138.     }
  139. }
  140.  
  141. // https://www.youtube.com/watch?v=w_jFUSIfDTU&list=PLzVLIdIx9dQxCKaiktxELrtXtnItgAAIr&index=8
  142. void solve()
  143. {
  144.     cin >> n;
  145.    
  146.     vset(g, n + 1, vpll(0));
  147.    
  148.     for(int i = 1; i < n; i++) {
  149.         int x, y, wt; cin >> x >> y >> wt;
  150.         g[x].pb({y, wt});
  151.         g[y].pb({x, wt});
  152.     }
  153.    
  154.     if(n == 1) {
  155.         cout << 0 << "\n";
  156.         return;
  157.     }
  158.    
  159.     dfs(1, 0);
  160.    
  161.     // as in prob. statement it is given to start traversing from node 1
  162.     cout << min(dp[1][0], dp[1][1]) << "\n";
  163. }
  164.  
  165. int main()
  166. {
  167.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  168.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  169.  
  170.     // #ifndef ONLINE_JUDGE
  171.     //     freopen("input.txt", "r", stdin);
  172.     //     freopen("output.txt", "w", stdout);
  173.     // #endif
  174.    
  175.     // #ifndef ONLINE_JUDGE
  176.     //      freopen("error.txt", "w", stderr);
  177.     // #endif
  178.    
  179.     int t = 1;
  180.     // int test = 1;
  181.     // cin >> t;
  182.     while(t--) {
  183.         // cout << "Case #" << test++ << ": ";
  184.         solve();
  185.     }
  186.  
  187.     return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement