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

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

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

We Tested Viral Kitchen Gadgets Here Is What You Must Know

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

How old is a 80 pound blue catfish?

Unveiling the Secrets Behind the Age of an 80-Pound Blue Catfish Catching a massive river monster is the ultimate dream for many freshwater anglers across...

Stop Buying Cheap Alternatives This Viral Kitchen Gadgets Is the Real Deal

📢 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!