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

Why Home Office Lighting Is Currently Breaking the Internet

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

What does rat scat look like?

Discovering unwanted pests in your living space is a homeowner's worst nightmare. Among the clearest signals of a rodent problem is finding fecal matter...

How to Use Snail Mucin Essence on Face (Complete Beginner’s Guide)

Overview: How to Use Snail Mucin Essence on Face (Complete Beginner's Guide) Finding reliable, data-backed guidance on how to use snail mucin essence on face...

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!