Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerEarth Optimal Edge Weights problem solution

YASH PAL, 31 July 2024
In this HackerEarth Optimal Edge Weights problem solution There is a state (territory) which is a tree rooted at node 1. All the cities (numbered from 1 to N + 1) in this state are connected via bidirectional roads. You have to add toll tax on each road. There are N roads which connect the cities of the state. You have to assign toll taxes on the roads so as to maximize the function Toll described below:
for(i=1;i<=number of cities;i++)
{
    for(j=i+1;j<=number of cities;j++)
    {
        toll+=(toll required to pay to travel from i to j)
     } 
}
You have to maximize the toll tax. Assign the roads by toll taxes from the given array A(using each value exactly once). Find the maximum toll obtained.
HackerEarth Optimal Edge Weights problem solution

HackerEarth Optimal Edge Weights problem solution.

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector<ll>tree[100005];
ll n;
vector<pair<ll,ll> >edglist;
vector<ll>v,v1;
ll vis[100005],subtree[100005],par[100005];
void dfs(ll node)
{
ll i,j;
vis[node]=1;
subtree[node]=1;
for(i=0;i<tree[node].size();i++)
{
j=tree[node][i];
if(!vis[j])
{
par[j]=node;
dfs(j);
subtree[node]+=subtree[j];
}
}
}
int main()
{
freopen("samp.txt","r",stdin);
freopen("sout.txt","w",stdout);
ll i,j,k,x,y;
cin>>n>>j;
edglist.clear();
for(i=1;i<=n;i++)
{
cin>>x>>y;
tree[x].push_back(y);
tree[y].push_back(x);
edglist.push_back({x,y});
}
dfs(1);
for(i=1;i<=n;i++)
{
cin>>j;
v.push_back(j);
}
sort(v.begin(),v.end());
for(i=0;i<n;i++)
{
x=edglist[i].first;
y=edglist[i].second;
if(par[x]==y)
{
v1.push_back(subtree[x]*(n+1-subtree[x]));
}
else
{
v1.push_back(subtree[y]*(n+1-subtree[y]));
}
}
sort(v1.begin(),v1.end());
ll ans=0;
for(i=0;i<v.size();i++)
{
ans+=(v[i]*v1[i]);
}
cout<<ans<<"n";
return 0;
}

Second solution

#include<bits/stdc++.h>
#define ll long long
using namespace std;
bool vis[200005];
ll sub[200005];
vector<int>v[200005];
vector<ll>vec;
ll ans;
void dfs(int node)
{
vis[node]=1;sub[node]++;
for(auto u: v[node])
{
if(!vis[u])
{
dfs(u);
sub[node]+=sub[u];
}
}
}
int main()
{
ll n,two;
cin>>n>>two;
for(int i=1;i<=n;i++)
{
int x,y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
ll w[200005];
for(int i=0;i<n;i++)
cin>>w[i];
dfs(1);
sort(w,w+n);
for(int i=2;i<=n+1;i++)
vec.push_back((n-sub[i]+1)*sub[i]);
sort(vec.begin(),vec.end());
for(int i=0;i<n;i++)
ans+=vec[i]*w[i];
cout<<ans<<"n";
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes