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);
}
}

The Outer Worlds 2...

Gamers and RPG enthusiasts have reason to celebrate as...

Why Travel Organization Gear...

📢 As an Amazon Associate, I earn from qualifying...

Why Everyone Is Talking...

📢 As an Amazon Associate, I earn from qualifying...

Why Everyone Is Talking...

📢 As an Amazon Associate, I earn from qualifying...

Why Everyone Is Talking...

📢 As an Amazon Associate, I earn from qualifying...

Why Everyone Is Talking...

📢 As an Amazon Associate, I earn from qualifying...

The Outer Worlds 2 v1.2.0.1-TENOKE

Gamers and RPG enthusiasts have reason to celebrate as Obsidian Entertainment's acclaimed franchise returns with new developments. The highly anticipated sequel to the award-winning...

Why Travel Organization Gear Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Travel Organization Gear?The Travel Organization Gear...

Why Everyone Is Talking About the Car Detailing Essentials

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Car Detailing Essentials?The Car Detailing Essentials...

Why Everyone Is Talking About the Smart Home Hacks

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Smart Home Hacks?The Smart Home Hacks...

Why Everyone Is Talking About the Viral Kitchen Gadgets

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Viral Kitchen Gadgets?The Viral Kitchen Gadgets...

Why Everyone Is Talking About the Travel Organization Gear

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Travel Organization Gear?The Travel Organization Gear...

Why Everyone Is Talking About the Travel Organization Gear

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Travel Organization Gear?The Travel Organization Gear...

GLP-1 Drugs Like Ozempic Carry Elevated Risks of Muscle Loss and Malnutrition in Adults Over 65

Understanding the Risks of GLP-1 Drugs for Older Adults GLP-1 receptor agonists, such as Ozempic, Wegovy, and Mounjaro, have transformed the landscape of weight management...

Renew your MSIG car insurance online in minutes — and get 10% off with promo PAULTAN

Renewing your car insurance is often perceived as a tedious chore, historically involving back-and-forth communication with agents, waiting for manual quotes, and dealing with...