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

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

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

📦 Looking for Codeforces Round 392 Div Problem Broken? Check the best deals on Amazon.

🛒 Shop Codeforces Round 392 Div Problem Broken on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

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

📦 Looking for Codeforces Round 392 Div Problem Broken? Check the best deals on Amazon.

🛒 Shop Codeforces Round 392 Div Problem Broken on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

How long should I bleach my live rock?

When resetting an aquarium or dealing with persistent pests like bubble algae, aiptasia, and unwanted parasites, bleaching live rock is often the most reliable...

Is Viral Kitchen Gadgets Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!