Codeforces Round #392 (Div. 2), problem: (E) Broken Tree Solution in C/C++

#include <bits/stdc++.h>
#define mk make_pair
using namespace std;
typedef long long ll;

const int N=1e6+5;

struct edge {
int x,y,w,p;
edge(){}
void input() {
scanf(“%d%d%d%d”,&x,&y,&w,&p);
}
}E[N],EE[N];

int n;
vector<int> G[N];
ll W[N],WW[N];

void dfs(int x,int y,int id,int mi) {
if(id) {
int tmp=EE[id].p-E[id].p;
tmp=min(mi,tmp);
E[id].p+=tmp;
E[id].w+=tmp;
mi=min(mi-tmp,E[id].p-(int)W[y]);
WW[id]=tmp;
}
for(int i=0;i<G[y].size();i++) {
int ID=G[y][i];
if(id==0) mi=2e9;
dfs(E[ID].x,E[ID].y,ID,mi);
mi-=WW[ID];
WW[id]+=WW[ID];
}
}

void dfs(int x,int y,int id) {
for(int i=0;i<G[y].size();i++) {
int ID=G[y][i];
dfs(E[ID].x,E[ID].y,ID);
}
if(id==0) return;
if(E[id].p<W[y]) {
puts(“-1”);
exit(0);
}
ll tmp=min(E[id].p-W[y],(ll)E[id].w-1);
E[id].p-=tmp;
E[id].w-=tmp;
W[x]+=W[y];
W[x]+=E[id].w;
}

int main() {
scanf(“%d”,&n);
for(int i=1;i<n;i++) {
E[i].input();
EE[i]=E[i];
G[E[i].x].push_back(i);
}
dfs(0,1,0);
dfs(0,1,0,2e9);
cout<<n<<endl;
for(int i=1;i<n;i++) {
printf(“%d %d %d %d\n”,E[i].x,E[i].y,E[i].w,E[i].p);
}
}

Oakley Brings Back the...

Oakley is renowned for pushing the boundaries of eyewear...

Kathy Griffin Shares She...

Comedian Kathy Griffin has never been one to shy...

Why Everyone is Obsessed with the Heavy Duty Duck Tape

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Oakley Brings Back the FatCat from the Archive

Oakley is renowned for pushing the boundaries of eyewear design, and now they are digging deep into their historical vault to bring back a...

Why Everyone is Obsessed with the Reusable Water Balloon Set

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Emergency Keychain Power Bank

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Kathy Griffin Shares She Was Hospitalized Over Complications From Colonoscopy

Comedian Kathy Griffin has never been one to shy away from sharing the raw, unfiltered details of her life. In her latest health update,...

Why Everyone is Obsessed with the Mini Handheld Pocket Fan

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Electric Power Scrubber Kit

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Mikie Sherrill wants Nets back in New Jersey, but team ‘perfectly happy in Brooklyn’

The political arena and the sports world have collided once again in the tri-state area. New Jersey's newly elected Governor, Mikie Sherrill, has raised...

Why Everyone is Obsessed with the Earbud Cleaning Pen Tool

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...