Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: D. Eternal Victory
- // Contest: Codeforces - Codeforces Beta Round #57 (Div. 2)
- // URL: https://codeforces.com/contest/61/problem/D
- // Memory Limit: 256 MB
- // Time Limit: 2000 ms
- // Parsed on: 10-09-2021 09:37:05 IST (UTC+05:30)
- // Author: Kapil Choudhary
- // ********************************************************************
- // कर्मण्येवाधिकारस्ते मा फलेषु कदाचन |
- // मा कर्मफलहेतुर्भूर्मा ते सङ्गोऽस्त्वकर्मणि || १.४७ ||
- // ********************************************************************
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define ld long double
- #define ull unsigned long long
- #define pb push_back
- #define ppb pop_back
- #define mp make_pair
- #define F first
- #define S second
- #define PI 3.1415926535897932384626
- #define sz(x) ((int)(x).size())
- #define vset(v, n, val) v.clear(); v.resize(n, val)
- typedef pair<int, int> pii;
- typedef pair<ll, ll> pll;
- typedef vector<int> vi;
- typedef vector<ll> vll;
- typedef vector<ull> vull;
- typedef vector<bool> vb;
- typedef vector<char> vc;
- typedef vector<string> vs;
- typedef vector<pii> vpii;
- typedef vector<pll> vpll;
- typedef vector<vi> vvi;
- typedef vector<vll> vvll;
- typedef vector<vull> vvull;
- typedef vector<vb> vvb;
- typedef vector<vc> vvc;
- typedef vector<vs> vvs;
- /************************************************** DEBUGGER *******************************************************************************************************/
- #ifndef ONLINE_JUDGE
- #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
- #else
- #define debug(x)
- #endif
- void _print(ll t) { cerr << t; }
- void _print(int t) { cerr << t; }
- void _print(string t) { cerr << t; }
- void _print(char t) { cerr << t; }
- void _print(ld t) { cerr << t; }
- void _print(double t) { cerr << t; }
- void _print(ull t) { cerr << t; }
- template <class T, class V> void _print(pair <T, V> p);
- template <class T> void _print(vector <T> v);
- template <class T> void _print(vector <vector<T>> v);
- template <class T> void _print(set <T> v);
- template <class T, class V> void _print(map <T, V> v);
- template <class T> void _print(multiset <T> v);
- template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
- template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
- 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; } }
- template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
- template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
- template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
- /*******************************************************************************************************************************************************************/
- mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
- int rng(int lim) {
- uniform_int_distribution<int> uid(0,lim-1);
- return uid(rang);
- }
- const int INF = 0x3f3f3f3f;
- const int mod = 1e9+7;
- ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
- while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
- ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
- ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
- /******************************************************************************************************************************/
- vector<vpll> g;
- // dp[i][0] = minimum possible traverse for subtree rooted at i if the path starts
- // from node i & ends at node located somewhere in the subtree rooted at i
- // dp[i][1] = minimum possible traverse for subtree rooted at i if the path starts as
- // well as ends at node i
- ll dp[100001][2];
- int n;
- bool is_leaf(int cur) {
- return (cur != 1) and (sz(g[cur]) == 1);
- }
- void dfs(int cur, int par) {
- // check for base case
- if(is_leaf(cur)) {
- dp[cur][0] = dp[cur][1] = 0LL;
- return;
- }
- // calculate the values for all the children of cur
- for(auto x: g[cur]) {
- if(x.F == par) continue;
- dfs(x.F, cur);
- }
- // calculate the values for cur
- dp[cur][1] = 0LL;
- for(auto x: g[cur]) {
- if(x.F == par) continue;
- dp[cur][1] += (x.S + dp[x.F][1] + x.S);
- }
- dp[cur][0] = LLONG_MAX;
- ll sum = dp[cur][1];
- for(auto x: g[cur]) {
- if(x.F == par) continue;
- // value of traverse if the path ends in subtree of child of cur
- ll val = sum - x.S - dp[x.F][1] + dp[x.F][0];
- dp[cur][0] = min(dp[cur][0], val);
- }
- }
- // https://www.youtube.com/watch?v=w_jFUSIfDTU&list=PLzVLIdIx9dQxCKaiktxELrtXtnItgAAIr&index=8
- void solve()
- {
- cin >> n;
- vset(g, n + 1, vpll(0));
- for(int i = 1; i < n; i++) {
- int x, y, wt; cin >> x >> y >> wt;
- g[x].pb({y, wt});
- g[y].pb({x, wt});
- }
- if(n == 1) {
- cout << 0 << "\n";
- return;
- }
- dfs(1, 0);
- // as in prob. statement it is given to start traversing from node 1
- cout << min(dp[1][0], dp[1][1]) << "\n";
- }
- int main()
- {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- srand(chrono::high_resolution_clock::now().time_since_epoch().count());
- // #ifndef ONLINE_JUDGE
- // freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- // #endif
- // #ifndef ONLINE_JUDGE
- // freopen("error.txt", "w", stderr);
- // #endif
- int t = 1;
- // int test = 1;
- // cin >> t;
- while(t--) {
- // cout << "Case #" << test++ << ": ";
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement