#include<bits/stdc++.h> using namespace std; const int N=400,M=(1e4+N)*2; int h[N],e[M],ne[M],f[M],l[M],idx; int q[N],d[N],cur[N]; int n,m; int S,T; int A[N]; void add(int a,int b,int c,int d) { ne[idx]=h[a],e[idx]=b,f[idx]=d-c,l[idx]=c,h[a]=idx++; ne[idx]=h[b],e[idx]=a,f[idx]=0,h[b]=idx++; } bool bfs() { memset(d,-1,sizeof(d)); int hh=0,tt=0; q[0]=S; d[S]=0; cur[S]=h[S]; while(hh<=tt) { int t=q[hh++]; for(int i=h[t];~i;i=ne[i]) { int ver=e[i]; if(d[ver]==-1&&f[i]) { d[ver]=d[t]+1; cur[ver]=h[ver]; if(ver==T) return true; q[++tt]=ver; } } } return false; } int find(int u,int limit) { if(u==T) return limit; int flow=0; for(int i=cur[u];~i&&flow<limit;i=ne[i]) { cur[u]=i; int ver=e[i]; if(d[ver]==d[u]+1&&f[i]) { int t=find(ver,min(f[i],limit-flow)); if(!t) d[ver]=-1; f[i]-=t,f[i^1]+=t,flow+=t; } } return flow; } int dinic() { int r=0; int flow; while(bfs()) while(flow=find(S,0x3f3f3f3f)) r+=flow; return r; } int main() { int sum=0; scanf("%d%d",&n,&m); memset(h,-1,sizeof(h)); S=0,T=n+1; for(int i=1;i<=m;i++) { int a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d); add(a,b,c,d); A[b]+=c; A[a]-=c; } for(int i=1;i<=n;i++) { if(A[i]>0) add(S,i,0,A[i]),sum+=A[i]; else if(A[i]<0) add(i,T,0,-A[i]); } if(dinic()==sum) { cout<<"YES"<<endl; for(int i=0;i<2*m;i+=2) { cout<<f[i^1]+l[i]<<endl; } }else { cout<<"NO"<<endl; } }