Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
626 views

in Online Assessments by Expert (115,900 points) | 626 views

2 Answers

0 like 0 dislike
Code for it -

 

int getMinServers(int n, vector<int> f, vector<int> t, vector<int> w, vector<int>v) {
using ll=long long;
vector<ll>sz(n),mx(n);
vector<vector<array<ll,2>>>a(n);
for(ll i=0;i<f.size();i++){
    f[i]--;
    t[i]--;
    a[f[i]].push_back({t[i],w[i]});
    a[t[i]].push_back({f[i],w[i]});
     
}
ll ans=0;
function<void(ll,ll)>dfs1=[&](ll u, ll p){
  sz[u]=1;
  for(auto[v,w]:a[u])  if(v!=p){
      mx[v]=max(mx[u]+w,w);
      dfs1(v,u);
      sz[u]+=sz[v];
  }
};
function<void(ll,ll)>dfs2=[&](ll u, ll p){
    if(u!=0 && mx[u]>v[u]) ans+=sz[u];
    else{
        for(auto [v,w]:a[u]) if(v!=p){
            dfs2(v,u);
        }
            }
};
dfs1(0,-1);
dfs2(0,-1);
return ans;
}
by Expert (115,900 points)
0 like 0 dislike

Good quality pics - 

 

by Expert (115,900 points)