Sort by

recency

|

13 Discussions

|

  • + 0 comments

    Here is my solution in java, python, C, C++, Csharp HackerRank Tree Flow Problem Solution

  • + 0 comments

    Here is the solution of Tree Flow Click here

  • + 0 comments

    Here is problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-tree-flow-problem-solution.html

  • + 0 comments

    c++

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    typedef pair<long long, long long> PII;
    
    long long n,x,y,z,i,ans1,ans2;
    vector<PII> edge[500007];
    
    void DFS1(long long bef, long long pos, long long sum) {
        long long i;
        for (i=0 ; i<edge[pos].size() ; i++) if (edge[pos][i].first != bef) {
            DFS1(pos,edge[pos][i].first,sum + edge[pos][i].second);
        }
        ans1 += sum;
    }
    
    void DFS2(long long bef, long long pos, long long sum) {
        long long i;
        for (i=0 ; i<edge[pos].size() ; i++) if (edge[pos][i].first != bef) {
            DFS2(pos,edge[pos][i].first,sum + edge[pos][i].second);
        }
        ans2 += sum;
    }
    
    int main() {
        scanf("%lld",&n);
        for (i=1 ; i<=n-1 ; i++) {
            scanf("%lld%lld%lld",&x,&y,&z);
            edge[x].push_back(PII(y,z));
            edge[y].push_back(PII(x,z));
        }
        
        DFS1(-1,1,0);
        DFS2(-1,n,0);
        //printf("%lld %lld\n",ans1,ans2);
        printf("%lld\n",min(ans1,ans2));
        
        return 0;
    }
    
  • + 1 comment

    https://zeroplusfour.com/tree-flow-hackerrank-solution/

    Here's how I did in all languages Java 8 , C++ , C , Python 3, Python 2.